Skip to content

Literals and classes

The shorthand classes, and what they are not

\w is not word characters. \s is wider than you think. The dot has a hole in it.

\d is [0-9]. \w is [A-Za-z0-9_]. \s is whitespace. Their capitals are the complements: \D, \W, \S.

\w is the one worth staring at. It is sixty-three characters, and it does not include an apostrophe, a hyphen, a dot, or any accented letter. It is not word characters; it is identifier characters in C, which is what it was named for.

\s is wider than a space and a tab. It includes the no-break space at U+00A0 and the byte order mark at U+FEFF — both invisible, both survivors of a copy-paste from a web page, and both the reason a field that looks empty fails an equality check.

The dot matches any character except a line terminator. That is four characters it will not match: \n, \r, and the two Unicode separators U+2028 and U+2029.

The s flag removes the exception. It is called dotAll, and it exists because . refusing to cross a line surprises people every single time.

Matcher

Every claim on the left has a pattern under it. Press one and it runs here, against the same engine that grades a task.