/^/

Regex Cheat Sheet

Every token, quantifier, and assertion you'll need — copyable and printable.

Anchors

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

Character Classes

TokenMeaningExample
.Any character except line breaksa.c
\dAny digit (0-9)\d{3}
\DAny non-digit\D+
\wAny word character (letter, digit, underscore)\w+
\WAny non-word character\W
\sAny whitespace character\s+
\SAny non-whitespace character\S+
[abc]Any of a, b, or c[abc]
[^abc]Any character except a, b, or c[^abc]
[a-z]Any character in the range a to z[a-z]+

Quantifiers

TokenMeaningExample
*Zero or more (greedy)ab*
+One or more (greedy)ab+
?Zero or one (optional)colou?r
{n}Exactly n times\d{4}
{n,}n or more times\d{2,}
{n,m}Between n and m times\d{2,4}
*?Zero or more (lazy)<.*?>
+?One or more (lazy)".+?"

Groups & Backreferences

TokenMeaningExample
(...)Capturing group(ab)+
(?:...)Non-capturing group(?:ab)+
(?<name>...)Named capturing group(?<year>\d{4})
\1Backreference to group 1(\w)\1
\k<name>Backreference to a named group(?<q>['"])\k<q>
|Alternation (OR)cat|dog

Lookaround Assertions

TokenMeaningExample
(?=...)Positive lookahead\d+(?=px)
(?!...)Negative lookahead\d+(?!px)
(?<=...)Positive lookbehind(?<=\$)\d+
(?<!...)Negative lookbehind(?<!\$)\d+

Escaping

TokenMeaningExample
\.Literal dot3\.14
\\Literal backslashC:\\\\Users
\/Literal forward slashhttps:\/\/
\(Literal opening parenthesis\(\d{3}\)
\[Literal opening bracket\[note\]

Unicode

TokenMeaningExample
\p{L}Any Unicode letter (needs u flag)\p{L}+
\p{N}Any Unicode number (needs u flag)\p{N}+
\p{Emoji}Any emoji character (needs u/v flag)\p{Emoji}
\u{1F600}Unicode code point escape (needs u flag)\u{1F600}

Flags

TokenMeaningExample
gGlobal — find all matches, not just the first
iCase-insensitive matching
mMultiline — ^ and $ match line boundaries
sDot-all — . also matches newlines
uUnicode — enables full Unicode matching
ySticky — matches only from lastIndex