Character Escapes
Character escapes act as shorthands for some common character classes.
Digit character — \d
The character escape \d matches digit characters, from 0 to 9. It is equivalent to the character class [0-9].
/\d/g- 4 matches
2020 - 6 matches
100/100 - 3 matches
It costs $5.45 - 6 matches
3.14159
/\d\d/g- 2 matches
2020 - 2 matches
100/100 - 1 match
It costs $5.45 - 2 matches
3.14159
\D is the negation of \d and is equivalent to [^0-9].
/\D/g- 0 matches
2020 - 1 match
100/100 - 11 matches
It costs $5.45 - 1 match
3.14159
Word character — \w
The escape \w matches characters deemed “word characters”. These include:
- lowercase alphabet —
a–z - uppercase alphabet —
A–Z - digits —
0–9 - underscore —
_
It is thus equivalent to the character class [a-zA-Z0-9_].
/\w/g- 6 matches
john_s - 7 matches
matej29 - 6 matches
Ayesha?! - 4 matches
4952 - 4 matches
LOUD - 4 matches
lo-fi - 6 matches
get out - 6 matches
21*2 = 42(1)
/\W/g- 0 matches
john_s - 2 matches
Ayesha?! - 0 matches
4952 - 0 matches
LOUD - 1 match
lo-fi - 1 match
get out - 3 matches
;-; - 6 matches
21*2 = 42(1)
Whitespace character — \s
The escape \s matches whitespace characters. The exact set of characters matched is dependent on the regex engine, but most include at least:
- space
- tab —
\t - carriage return —
\r - new line —
\n - form feed —
\f
Many also include vertical tabs (\v). Unicode-aware engines usually match all characters in the separator category.
The technicalities, however, will usually not be important.
/\s/g- 1 match
word word - 2 matches
tabs vs spaces - 0 matches
snake_case.jpg
/\S/g- 8 matches
word word - 12 matches
tabs vs spaces - 14 matches
snake_case.jpg
Any character — .
While not a typical character escape, . matches any1 character.
/./g- 6 matches
john_s - 8 matches
Ayesha?! - 4 matches
4952 - 4 matches
LOUD - 5 matches
lo-fi - 7 matches
get out - 3 matches
;-; - 12 matches
21*2 = 42(1)
- Except the newline character
\n. This can be changed using the “dotAll” flag, if supported by the regex engine in question.↩