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[RegExr] [Visual]- 4 matches
20202020
- 6 matches
100/100100100
- 3 matches
It costs $5.45545
- 6 matches
3.14159314159
/\d\d/g[RegExr] [Visual]- 2 matches
20202020
- 2 matches
100/1001010
- 1 match
It costs $5.4545
- 2 matches
3.141591415
\D is the negation of \d and is equivalent to [^0-9].
/\D/g[RegExr] [Visual]- 0 matches
2020 - 1 match
100/100/
- 11 matches
It costs $5.45Itcosts$.
- 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[RegExr] [Visual]- 6 matches
john_sjohn_s
- 7 matches
matej29matej29
- 6 matches
Ayesha?!Ayesha
- 4 matches
49524952
- 4 matches
LOUDLOUD
- 4 matches
lo-filofi
- 6 matches
get outgetout
- 6 matches
21*2 = 42(1)212421
/\W/g[RegExr] [Visual]- 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[RegExr] [Visual]- 1 match
word word - 2 matches
tabs vs spaces - 0 matches
snake_case.jpg
/\S/g[RegExr] [Visual]- 8 matches
word wordwordword
- 12 matches
tabs vs spacestabsvsspaces
- 14 matches
snake_case.jpgsnake_case.jpg
Any character — .
While not a typical character escape, . matches any1 character.
/./g[RegExr] [Visual]- 6 matches
john_sjohn_s
- 8 matches
Ayesha?!Ayesha?!
- 4 matches
49524952
- 4 matches
LOUDLOUD
- 5 matches
lo-filo-fi
- 7 matches
get outgetout
- 3 matches
;-;;-;
- 12 matches
21*2 = 42(1)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.↩