Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert space before capital letters

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

How can I do this in jQuery or JavaScript?

like image 441
CLiown Avatar asked Apr 07 '11 13:04

CLiown


People also ask

How do you put a space before a capital letter in a string?

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.

Is space uppercase or lowercase?

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.


1 Answers

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() 
like image 71
user2051552 Avatar answered Sep 25 '22 11:09

user2051552