Add a space between two words

前端 未结 3 2028
粉色の甜心
粉色の甜心 2021-01-12 23:38

I have some words like \"Light Purple\" and \"Dark Red\" which are stored as \"LightPurple\" and \"DarkRed\". How do I check for the uppercase letters in the word like \"Lig

3条回答
  •  佛祖请我去吃肉
    2021-01-13 00:33

    You can use a regex to add a space wherever there is a lowercase letter next to an uppercase one.

    Something like this:

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

    UPDATE: If you have more than 2 words, then you'll need to use the g flag, to match them all.

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

    UPDATE 2: If are trying to split words like CSVFile, then you might need to use this regex instead:

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

提交回复
热议问题