-2

I have constructed a string as follows

let str = String.fromCharCode(13) + ' ';

Next, I would like to know if the string contains two spaces

str.match(/\s\s/)

The weird thing is that it is a match. But if I do

str.match(/  /)

it isn't. Can someone explain to me why this is happening ?

Jeanluca Scaljeri
  • 19,619
  • 37
  • 147
  • 259
  • 1
    A space in a regex pattern (matches a regular space) is not the same as `\s` (any whitespace). – Wiktor Stribiżew Oct 06 '16 at 14:40
  • 1
    see [here](http://www.regular-expressions.info/shorthand.html) for example. `\s` is equivalent to `[ \t\r\n\f]`. In other words, it matched space, tab, new line, form feed and carriage return. Note it might vary slightly with different flavors of regex. – Matt Burland Oct 06 '16 at 14:42

1 Answers1

1

The '\s' pattern allows you to match any kind of whitespace, not just space itself. For a more detailed list you can check here for example.

For reference (copied from the developer reference):

Matches a single white space character, including space, tab, form feed, line feed and other Unicode spaces. Equivalent to [ \f\n\r\t\v\u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff].

For example, /\s\w*/ matches " bar" in "foo bar".

Neikos
  • 1,574
  • 1
  • 19
  • 32