42

I am learning javascript and I am analyzing existing codes.

In my JS reference book, it says to search on a single space use "\s"?

But I have came across the code

obj.match(/Kobe Bryant/);

Instead of using \s, it uses the actual space?

Why doesn't this generate an error?

caitriona
  • 8,569
  • 4
  • 32
  • 36
Dennis D
  • 1,293
  • 4
  • 17
  • 24
  • The MDN RegExp page is a good resource for JS regular expression and dealing with the `RegExp` object: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp –  Jan 16 '15 at 05:48

5 Answers5

87

The character class \s does not just contain the space character but also other Unicode white space characters. \s is equivalent to this character class:

[\t\n\v\f\r \u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000]
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • 3
    +1, probably took you longer than the others to fetch the link :-) – Andy E Mar 11 '10 at 22:39
  • 3
    It is incredibly important for anybody who is encoding data to consider the difference between "the one true space char, ' ', and 'any whitespace user might send me'. \s is almost always what you want, but be careful... if you are encoding binary data, be sure to match only 'spacebar' `\u0020` – Ajax Dec 21 '17 at 11:46
  • The link is no longer valid. Could you please update it? I'm really interested. Thanks. – Krisztián Balla Jan 09 '20 at 08:11
15

No. It is perfectly legal to include a literal space in a regex.

However, it's not equivalent - \s will include any whitespace character, including tabs, non-breaking spaces, half-width spaces and other characters, whereas a literal space will only match the regular space character.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
10

\s matches any whitespace character, including tabs etc. Sure you can use a literal space also without problems. Just like you can use [0-9] instead of \d to denote any digit. However, keep in mind that [0-9] is equivalent to \d whereas the literal space is a subset of \s.

76484
  • 8,498
  • 3
  • 19
  • 30
Tatu Ulmanen
  • 123,288
  • 34
  • 187
  • 185
4

In addition to normal spaces, \s matches different kinds of white space characters, including tabs (and possibly newline characters, according to configuration). That said, matching with a normal space is certainly valid, especially in your case where it seems you want to match a name, which is normally separated by a normal space.

Hosam Aly
  • 41,555
  • 36
  • 141
  • 182
0

\xA0 is also a white space, especially a non-breaking space. We did have an issue with Quill Editor when we paste a plain text with spaces from notepad++. The white space in JS \xA0 is a white space but does not match a normal white space \u0020 (' ').

user1587368
  • 314
  • 2
  • 6