Convert camelCaseText to Sentence Case Text

后端 未结 20 2208
闹比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:25

    var text = 'helloThereMister';
    var result = text.replace( /([A-Z])/g, " $1" );
    var finalResult = result.charAt(0).toUpperCase() + result.slice(1);
    console.log(finalResult);
    

    capitalize the first letter - as an example.

    Note the space in " $1".

    EDIT: added an example of capitalization of the first letter. Of course, in case the first letter is already capital - you would have a spare space to remove.

提交回复
热议问题