Regex Cheat Sheet
A regular-expression reference, character classes, anchors, quantifiers, groups and alternation, lookarounds, common ready-made patterns, and flags.
A reference for regular-expression syntax common to most engines (JavaScript, Python,
PCRE). To test a pattern interactively, try our regex tester. To match a literal
metacharacter, escape it with a backslash, e.g. \. for a dot.
Character classes
| Token | Matches |
|---|---|
. | Any character except newline |
\d / \D | A digit / a non-digit |
\w / \W | Word char (a–z, 0–9, _) / non-word char |
\s / \S | Whitespace / non-whitespace |
[abc] | Any one of a, b, or c |
[^abc] | Any character except a, b, or c |
[a-z0-9] | A range: any lowercase letter or digit |
Anchors & boundaries
| Token | Matches |
|---|---|
^ | Start of string (or line in multiline mode) |
$ | End of string (or line in multiline mode) |
\b / \B | Word boundary / non-boundary |
Quantifiers
| Token | Matches |
|---|---|
* | 0 or more (greedy) |
+ | 1 or more |
? | 0 or 1 (optional) |
{3} | Exactly 3 |
{2,5} | Between 2 and 5 |
{2,} | 2 or more |
*? +? | Lazy: match as few as possible |
Groups & alternation
| Token | Meaning |
|---|---|
(abc) | Capturing group (referenced as \1) |
(?:abc) | Non-capturing group |
(?<name>abc) | Named capturing group |
a|b | Match a or b |
(?=abc) | Lookahead: followed by abc |
(?!abc) | Negative lookahead: not followed by abc |
(?<=abc) | Lookbehind: preceded by abc |
Common patterns
| Pattern | Matches |
|---|---|
^\d{5}(-\d{4})?$ | US ZIP code (5 or 9 digit) |
[\w.+-]+@[\w-]+\.[\w.-]+ | Email address (rough) |
https?://[^\s]+ | An http or https URL |
^\d{4}-\d{2}-\d{2}$ | Date in YYYY-MM-DD form |
\b\d{1,3}(,\d{3})*\b | Number with thousands separators |
Common flags: g (global, find all), i
(case-insensitive), m (multiline, ^/$ match each
line), and s (dot also matches newlines). Greedy quantifiers can backtrack a
lot; prefer lazy versions or specific character classes for speed.
What this does
A regex cheat sheet lists the building blocks of regular expressions — character classes, anchors, quantifiers, groups, alternation, lookarounds, and flags.
How to use it
- Browse by token type.
- Find the pattern you need.
- Copy it with one tap.
- Test it against your text.
Example
^\d{3}-\d{4}$ matches a 7-digit phone like 555-1234.
Sources & methodology
Last updated .
Frequently asked questions
Does this work for JavaScript and Python?
Yes, the syntax shown is common to most modern engines, including JavaScript, Python, PCRE, and Java. A few advanced features differ between flavors.
How do I match a literal special character?
Escape it with a backslash. For example, \. matches a literal dot and \( matches a literal open parenthesis.
What is the difference between greedy and lazy?
Greedy quantifiers (like .*) match as much as possible; adding a ? makes them lazy (.*?), matching as little as possible. Lazy versions are often faster and more precise.
Related tools
Git Cheat Sheet
The Git commands you use every day, setup, the add/commit/push workflow, branching and merging, working with remotes, and undoing mistakes safely.
Docker Cheat Sheet
A reference for everyday Docker, building and tagging images, running and managing containers, ports and volumes, system cleanup, and Docker Compose.
Vim Cheat Sheet
A reference for the Vim editor, modes, moving around, editing, search and replace, and saving or quitting, including the escape hatch for when you are stuck.
SQL Cheat Sheet
A reference for standard SQL, querying with SELECT, filtering with WHERE, aggregates and GROUP BY, joining tables, and modifying data and schema.