Remove all spaces from a string using JavaScript?

yaboy01 asked this question 21 days ago
yaboy01 21 days ago

When I'm dealing with strings in JavaScript, I'm looking for a way to remove all spaces they may or may not contain.

This includes leading/trailing spaces and any spaces inside the string.

How can I do this?

3 suggested answers
coderguy 11 days ago

You can remove any leading or trailing spaces with trim():


And you can remove any spaces inside the string with this replace() and regular expression combination:

string.replace(/\s/g, "")
0 replies
nick 11 days ago
string = string.split(" ").join("")
0 replies
looper003 11 days ago
string.replace(/ /g, '')
0 replies