Confusion of regular expressions \ s and ""

In the regex, I know when use \ s represents a space, but in the following case they will be different:

  • / a \ sb / --- with \ s
  • / ab / --- with an empty field

Thanks so much if you can explain to me.

+5
source share
1 answer

The \ s character class matches all whitespace characters, not just spaces. This includes tabs (\ t), and if multi-line matching is enabled, it includes a carriage return (\ r) and a new line (\ n). Theoretically, if your regex engine handles unicode, there are also unicode space characters that may match \, although your mileage may vary.

, "a\t b" regex/a\s + b/, .

+15

All Articles