Regex Cheatsheet

A quick reference for regular-expression syntax. Every token with its meaning and an example.

Anchors

TokenMeaningExample
^Start of string (or line with m)^Hello
$End of string (or line with m)world$
\bWord boundary\bcat\b
\BNot a word boundary\Bcat

Character classes

TokenMeaningExample
.Any character except newlinea.c
\dAny digit, 0–9\d\d
\DAny non-digit\D+
\wAny word character (a–z, A–Z, 0–9, _)\w+
\WAny non-word character\W
\sAny whitespace charactera\sb
\SAny non-whitespace character\S+
[abc]Any one character in the set[aeiou]
[^abc]Any character not in the set[^0-9]
[a-z]Any character in the range a to z[A-Z]

Quantifiers

TokenMeaningExample
*Zero or more of the precedingab*
+One or more of the precedingab+
?Zero or one (optional)ab?
{n}Exactly n of the preceding\d{4}
{n,}n or more of the preceding\d{2,}
{n,m}Between n and m of the preceding\d{2,4}
*?Lazy — match as few as possible<.*?>

Groups & lookaround

TokenMeaningExample
(abc)Capturing group(\d+)
(?:abc)Non-capturing group(?:ab)+
(?<name>)Named capturing group(?<year>\d{4})
a|bAlternation — a or bcat|dog
(?=abc)Positive lookahead\d(?=px)
(?!abc)Negative lookahead\d(?!px)
(?<=abc)Positive lookbehind(?<=\$)\d+
(?<!abc)Negative lookbehind(?<!\$)\d+
\1Backreference to group 1(\w)\1

Special & escapes

TokenMeaningExample
\.A literal dot (escape special chars)3\.14
\uXXXXUnicode character by code pointé

Flags

TokenMeaningExample
gGlobal — find all matches/\d+/g
iCase-insensitive matching/abc/i
mMultiline — ^ and $ match line breaks/^x/m
sDotall — . also matches newlines/a.b/s
uUnicode mode/\u{1F600}/u
ySticky — match from lastIndex only/\d/y

Try it yourself

Head to the tester to experiment with any of these tokens live.

Open the Regex Tester