Regex Cheat Sheet

47 regular expression tokens across 8 categories — every anchor, character class, quantifier, group, assertion, escape, and flag, each with an example. Copyable, printable, and grouped so you can find what you need without scrolling through prose.

Want the explanation rather than the reference? Read theregular expressions guide, or try a token in theregex tester.

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

Regex Syntax Questions

What is the difference between * and + in regex?

Both are quantifiers applied to the item before them. The asterisk means "zero or more", so it can match nothing at all, while the plus means "one or more" and requires at least one occurrence. That is why /ab*c/ matches "ac" but /ab+c/ does not.

What does \b mean in a regular expression?

\b matches a word boundary — the zero-width position between a word character (a letter, digit, or underscore) and anything else, including the start or end of the string. It consumes no characters, which is why /\bcat\b/ matches "cat" in "the cat sat" but not inside "concatenate".

What is the difference between greedy and lazy quantifiers?

A greedy quantifier such as .* takes as much text as it can and then gives characters back until the rest of the pattern fits. A lazy quantifier, written by adding a question mark as in .*?, takes as little as possible and expands only when it has to. Greedy matching is the default and is the usual cause of a pattern matching far more text than intended.

How do I escape a special character in regex?

Put a backslash in front of it. The characters that need escaping outside a character class are . ? * + ( ) [ ] { } | ^ $ / and the backslash itself. Inside a character class most of these are already literal — only ^ (at the start), ] , - , and \ need care.

Can I print this regex cheat sheet?

Yes. Use the print button at the top of the page — the layout is styled for print, so the tables fit cleanly on paper without the navigation, sidebar, or footer. It also works as a PDF via your browser's print dialog.