rain zwr
rain zwr

Reputation: 111

regular expression confusion \s and " "

In regular expression, i know when use \s to represent a space, but, in following case, would they be different:

  1. /a\sb/ ---with a \s
  2. /a b/ ---with empty field

thanks a lot if you can explain to me.

Upvotes: 5

Views: 7101

Answers (1)

Nick White
Nick White

Reputation: 2061

The \s character class matches all "whitespace characters," not just spaces. This includes tabs (\t), and if multiline matching is allowed, it includes carriage return (\r) and newline (\n). Theoretically, if your regular expression engine handles unicode, there are also unicode whitespace characters that \s can match, though your mileage may vary.

So with a string like "a\t b", you can match it with the regex /a\s+b/, in case that is useful to you.

Upvotes: 15

Related Questions