Insert space before capital letters

前端 未结 8 1199
误落风尘
误落风尘 2020-12-02 07:01

I have a string \"MySites\". I want to place a space between My and Sites.

How can I do this in jQuery or JavaScript?

8条回答
  •  旧时难觅i
    2020-12-02 07:55

    You can just add a space before every uppercase character and trim off the leading and trailing spaces

    s = s.replace(/([A-Z])/g, ' $1').trim()
    

提交回复
热议问题