Regex to split camel case

前端 未结 12 1841
逝去的感伤
逝去的感伤 2020-11-28 23:31

I have a regular expression in JavaScript to split my camel case string at the upper-case letters using the following code (which I subsequently got from here):



        
12条回答
  •  渐次进展
    2020-11-28 23:56

    If you want an array of lower case words:

    "myCamelCaseString".split(/(?=[A-Z])/).map(s => s.toLowerCase());
    

    If you want a string of lower case words:

    "myCamelCaseString".split(/(?=[A-Z])/).map(s => s.toLowerCase()).join(' ');
    

    If you want to separate the words but keep the casing:

    "myCamelCaseString".replace(/([a-z])([A-Z])/g, '$1 $2')
    

提交回复
热议问题