Split by Caps in Javascript

前端 未结 4 1486
说谎
说谎 2020-12-01 11:57

I am trying to split up a string by caps using Javascript,

Examples of what Im trying to do:

\"HiMyNameIsBob\"  ->   \"Hi My Name Is Bob\"
\"Greet         


        
4条回答
  •  星月不相逢
    2020-12-01 12:16

    To expand on Rob W's answer.

    This takes care of any sentences with abbreviations by checking for preceding lower case characters by adding [a-z], therefore, it doesn't spilt any upper case strings.

    // Enter your code description here
     var str = "THISSentenceHasSomeFunkyStuffGoingOn. ABBREVIATIONSAlsoWork.".split(/(?=[A-Z][a-z])/).join(" ");  // -> "THIS Sentence Has Some Funky Stuff Going On. ABBREVIATIONS Also Work."
     console.log(str);

提交回复
热议问题