Convert camelCaseText to Sentence Case Text

后端 未结 20 2211
闹比i
闹比i 2020-11-28 03:44

How can I convert a string either like \'helloThere\' or \'HelloThere\' to \'Hello There\' in JavaScript?

20条回答
  •  情书的邮戳
    2020-11-28 04:33

    Adding yet another ES6 solution that I liked better after not being happy with a few thoughts above.

    https://codepen.io/902Labs/pen/mxdxRv?editors=0010#0

    const camelize = (str) => str
        .split(' ')
        .map(([first, ...theRest]) => (
            `${first.toUpperCase()}${theRest.join('').toLowerCase()}`)
        )
        .join(' ');
    

提交回复
热议问题