I have a string "MySites"
. I want to place a space between My
and Sites
.
How can I do this in jQuery or JavaScript?
Use the replace() method to insert a space before the capital letters in a string, e.g. str. replace(/[A-Z]/g, ' $&'). trim() . The replace method will return a new string, where each capital letter is replaced by a space preceding the capital letter.
No, don't capitalize it. Most celestial bodies (Saturn, Sirius, etc etc) get capitalized, but space doesn't. Don't capitalize it in compound words and phrases such as spacecraft, spaceman, spaceship, spacesuit, space station, space vehicle, space walk, and space-time either.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With