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 a line break (JavaScript excludes \n, \r, U+2028 and U+2029; Python excludes only \n). With the s flag it matches line breaks too |
\d / \D | A digit / a non-digit |
\w / \W | Word char / non-word char. In JavaScript \w is [A-Za-z0-9_], so it skips accented and non-Latin letters (use a \p{…} property escape with the u flag for those); Python str patterns also match Unicode letters and digits |
\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). Python and PCRE also match just before a newline at the very end of the string; JavaScript does not |
\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 (Python spells it (?P<name>abc)) |
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 JavaScript flags: g (global, find all), i
(case-insensitive), m (multiline, ^/$ match each
line), and s (dot also matches newlines). Python has no g flag: use
re.findall or re.finditer to get every match, and re.I,
re.M and re.S (or inline (?i), (?m),
(?s)) for the others.
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.
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, which is what you want when the shortest match is the right one.
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.