Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regular expression confusion \s and " "

Tags:

regex

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.

like image 604
rain zwr Avatar asked Jun 01 '12 00:06

rain zwr


1 Answers

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.

like image 130
Nick White Avatar answered Sep 25 '22 22:09

Nick White