93

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

Šime Vidas
  • 163,768
  • 59
  • 261
  • 366
  • 1
    vidas http://stackoverflow.com/questions/5963182/how-to-remove-spaces-from-a-string-using-javascript/5963256 I bet this is what made you ask this question ;) (your comment on the answer) – rsplak May 11 '11 at 12:46
  • 2
    I can imagine `\s+` is faster, because it can replace chunks of whitespace, whilst `\s` must replace each white space separately? – KooiInc May 11 '11 at 12:48
  • 1
    @KooiInc: Correct, because it matches/replaces fewer times. – BoltClock May 11 '11 at 12:49

4 Answers4

231

In the first regex, each space character is being replaced, character by character, with the empty string.

In the second regex, each contiguous string of space characters is being replaced with the empty string because of the +.

However, just like how 0 multiplied by anything else is 0, it seems as if both methods strip spaces in exactly the same way.

If you change the replacement string to '#', the difference becomes much clearer:

var str = '  A B  C   D EF ';
console.log(str.replace(/\s/g, '#'));  // ##A#B##C###D#EF#
console.log(str.replace(/\s+/g, '#')); // #A#B#C#D#EF#
BoltClock
  • 630,065
  • 150
  • 1,295
  • 1,284
28

\s means "one space", and \s+ means "one or more spaces".

But, because you're using the /g flag (replace all occurrences) and replacing with the empty string, your two expressions have the same effect.

Lightness Races in Orbit
  • 358,771
  • 68
  • 593
  • 989
  • But the second version will probably be faster. – Tim Pietzcker May 11 '11 at 12:43
  • does it means, I need not to use /g if I am using \s+ – Gaurav May 11 '11 at 12:45
  • 1
    @Gaurav: No. Because then it would replace only the first `\s+`, leaving the rest intact. For example, `' foo bar '.replace(/\s+/, '')` would give you only `'foo bar '` *edit argh HTML condensing two spaces into one* – BoltClock May 11 '11 at 12:45
  • @Gaurav I don't think so. If you omit the `g` modifier, only the first occurence of a block of whitespaces will be replaced. – KooiInc May 11 '11 at 12:50
12

In a match situation the first would return one match per whitespace, when the second would return a match for each group of whitespaces.

The result is the same because you're replacing it with an empty string. If you replace it with 'x' for instance, the results would differ.

str.replace(/\s/g, 'x') will return 'xxAxBxxCxxxDxEF '

while str.replace(/\s+/g, 'x') will return 'xAxBxCxDxEF '

because \s matches each whitespace, replacing each one with 'x', and \s+ matches groups of whitespaces, replacing multiple sequential whitespaces with a single 'x'.

Doug
  • 6,097
  • 3
  • 26
  • 47
3

+ means "one or more characters" and without the plus it means "one character." In your case both result in the same output.

Josh M.
  • 23,573
  • 23
  • 96
  • 160