Character classes — [abc], [^abc], \d \w \s and .
FlagA class matches one character from a set.
| Syntax | Matches one… |
|---|---|
[aeiou], [a-z0-9] | listed character, or one in a range |
[^0-9] | character not in the set (^ first inside the brackets) |
\d / \D | digit [0-9] / anything else |
\w / \W | word character [A-Za-z0-9_] / anything else |
\s / \S | whitespace, including tabs, newlines and Unicode spaces / anything else |
. | character except a line terminator (\n, \r, \u2028, \u2029), unless the s flag is on |
\d and \w are ASCII, even with u: /\w/u.test("é") and /\d/u.test("٣") are false.
(With iu or iv, \w also matches ſ and the Kelvin sign U+212A.) For any script, use
/\p{L}/u or /\p{Nd}/u.
Inside a class most metacharacters are literal — [.] matches a dot.