When we have a string that contains space characters:
var str = ' A B C D EF ';
and we want to remove the spaces from the string (we want this: 'ABCDEF'
).
Both this:
str.replace(/\s/g, '')
and this:
str.replace(/\s+/g, '')
will return the correct result.
Does this mean that the +
is superfluous in this situation? Is there a difference between those two regular expressions in this situation (as in, could they in any way produce different results)?
Update: Performance comparison - /\s+/g
is faster. See here: http://jsperf.com/s-vs-s
\s+
is faster, because it can replace chunks of whitespace, whilst\s
must replace each white space separately? – KooiInc