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
2020
2
0
2
0
- 6 matches
100/100
1
0
0
1
0
0
- 3 matches
It costs $5.45
5
4
5
- 6 matches
3.14159
3
1
4
1
5
9
/\d\d/g
[RegExr] [Visual]- 2 matches
2020
20
20
- 2 matches
100/100
10
10
- 1 match
It costs $5.45
45
- 2 matches
3.14159
14
15
\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.45
I
t
c
o
s
t
s
$
.
- 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_s
j
o
h
n
_
s
- 7 matches
matej29
m
a
t
e
j
2
9
- 6 matches
Ayesha?!
A
y
e
s
h
a
- 4 matches
4952
4
9
5
2
- 4 matches
LOUD
L
O
U
D
- 4 matches
lo-fi
l
o
f
i
- 6 matches
get out
g
e
t
o
u
t
- 6 matches
21*2 = 42(1)
2
1
2
4
2
1
/\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 word
w
o
r
d
w
o
r
d
- 12 matches
tabs vs spaces
t
a
b
s
v
s
s
p
a
c
e
s
- 14 matches
snake_case.jpg
s
n
a
k
e
_
c
a
s
e
.
j
p
g
Any character — .
While not a typical character escape, .
matches any1 character.
/./g
[RegExr] [Visual]- 6 matches
john_s
j
o
h
n
_
s
- 8 matches
Ayesha?!
A
y
e
s
h
a
?
!
- 4 matches
4952
4
9
5
2
- 4 matches
LOUD
L
O
U
D
- 5 matches
lo-fi
l
o
-
f
i
- 7 matches
get out
g
e
t
o
u
t
- 3 matches
;-;
;
-
;
- 12 matches
21*2 = 42(1)
2
1
*
2
=
4
2
(
1
)
- Except the newline character
\n
. This can be changed using the “dotAll” flag, if supported by the regex engine in question.↩