Convert camelCaseText to Sentence Case Text

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

    I had a similar problem and dealt with it like this:

    stringValue.replace(/([A-Z]+)*([A-Z][a-z])/g, "$1 $2")
    

    For a more robust solution:

    stringValue.replace(/([A-Z]+)/g, " $1").replace(/([A-Z][a-z])/g, " $1")
    

    http://jsfiddle.net/PeYYQ/

    Input:

     helloThere 
     HelloThere 
     ILoveTheUSA
     iLoveTheUSA
    

    Output:

     hello There 
     Hello There 
     I Love The USA
     i Love The USA
    

提交回复
热议问题