How can I replace space with another string in JavaScript? -


i have code:

var str = '                  abc'; str.replace(" ",""); 

but not working.

first, have spelling error:

replce 

you should do:

var str = ' abc'; str = str.replace(/ /g,""); 

the /g global modifier replace specified text throughout given string.


alternatively, can use split , join this:

var str = ' abc'; str = str.split(" ").join(""); 

Comments