Reputation: 4408
I want to replace two or more occurrences of whitespace with just one whitespace character using JavaScript (this code is going to sit inside a chrome extension).
Upvotes: 3
Views: 3081
Reputation: 9172
You can do both at once with:
"str str\t\t\tstr".replace(/(\s)+/g, "$1");
Upvotes: 3
Reputation: 2339
For spaces:
'test with spaces'.replace( /\s+/g, ' ' );
For tabs:
'test\t\t\twith\t\t\ttabs'.replace( /\t+/g, '\t' );
Upvotes: 1
Reputation: 45555
" this is tooo spaced ".replace(/\s+/g, " ");
returns
" this is tooo spaced "
Upvotes: 4