zsola3075457
zsola3075457

Reputation: 177

javascript - How to reduce extra white spaces except line breaks

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

Answers (1)

anubhava
anubhava

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

Related Questions