Insert space before capital letters

CLiown picture CLiown · Apr 7, 2011 · Viewed 66.3k times · Source

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

How can I do this in jQuery or JavaScript?

Answer

user2051552 picture user2051552 · Aug 22, 2014

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()