Reputation: 177
replace(/\s+/g,' ');
reduces all whitespace to one space, including breaks. Sometime I have more than 1 spaces what I want to reduce to one, keeping the new lines, breaks.
Upvotes: 2
Views: 306
Reputation: 784938
You should use:
string = string.replace(/ {2,}/g, ' ');
\s
matches all white-spaces including newlines.OR using lookahead:
string = string.replace(/ +(?= )/g, '');
Upvotes: 2