Regular Expressions Cheat Sheet
Anchors
^
: start of the string or the start of a line in a multiline pattern$
: end of the string or the end of a line in a multiline pattern\b
: word boundary\B
: not word boundary (opposite of\b
)
Character sequences
.
: any character except line breaks\w
: any word character\W
: any non-word character (opposite of\w
)\s
: any whitespace character\S
: any non-whitespace character (opposite of\s
)\d
: any digit character\D
: any non-digit character (opposite of\d
)[abc]
: a single character in the given set (herea
,b
orc
)[^abc]
: a single character not in the given set (opposite of[abc]
)[a-z]
: a single character in the given range (here betweena
andz
inclusive)[^a-z]
: a single character not in the given range (opposite of[a-z]
)[a-zA-Z]
: a single character in either of the given ranges
Quantifiers
a?
: zero or one ofa
(equal toa{0,1}
)a*
: zero or more ofa
(equal toa{0,}
)a+
: one or more ofa
(equal toa{1,}
)a{3}
: exactly 3 ofa
a{3,}
: 3 or more ofa
a{3,5}
: between 3 and 5 ofa
(inclusive)
Groups
(ab)
: match and capture everything enclosed (here exactlyab
)(a|b)
: match and capture either one character (herea
orb
)(?:ab)
: match everything enclosed, without capturing
Flags
g
: Globalm
: Multilinei
: Case insensitiveu
: Unicode
Note that this cheatsheet is meant only as a starting point and is by no means a complete guide to all the features and nuances of regular expressions. You can also read 6 JavaScript Regular Expression features you can use today for a deeper dive into some more advanced features.