Regex Cheatsheet

Perl/JS-compatible regex essentials. Works in most languages with tweaks.

1 credit

Character classes

5 items
Any char (not newline)
.
Word char [A-Za-z0-9_]
\w / \W (not word)
Digit
\d / \D (not digit)
Whitespace
\s / \S (not whitespace)
Custom set
[abc] [^abc] [a-z]

Quantifiers

6 items
0 or more
*
1 or more
+
0 or 1
?
Exactly n
{n}
n to m
{n,m}
Lazy (non-greedy)
*? +? ??

Anchors & boundaries

2 items
Start / end of string
^ $
Word boundary
\b \B

Groups

5 items
Capture
(abc)
Non-capture
(?:abc)
Named
(?<name>abc)
Backref
\1 or \k<name>
Lookahead / behind
(?=...) (?!...) (?<=...) (?<!...)

Flags (JS)

5 items
Case-insensitive
i
Multiline (^/$ per line)
m
Dot matches newline
s
Global (all matches)
g
Unicode
u

Further reading