remove extra spaces in string javascript
You're close.
Remember that replace
replaces the found text with the second argument. So:
newString = string.replace(/\s+/g,''); // "thiscontainsspaces"
Finds any number of sequential spaces and removes them. Try replacing them with a single space instead!
newString = string.replace(/\s+/g,' ').trim();
string.replace(/\s+/g, ' ').trim()
I figured out one way, but am curious if there is a better way...
string.replace(/\s+/g,' ').trim()