Regex cheatsheet
Every regex symbol on one page, described in plain English. Bookmark it, keep it next to your editor,
and test any pattern live when in doubt.
Character classes
| . | Any character except newline (any at all with flag s) |
| \d \D | Digit 0–9 / not a digit |
| \w \W | Word char (letter, digit, _) / not a word char |
| \s \S | Whitespace / not whitespace |
| [abc] | One of a, b or c |
| [^abc] | Any character except a, b, c |
| [a-z0-9] | Character ranges |
| \p{L} | Unicode letter (needs flag u) |
Anchors & boundaries
| ^ | Start of string (start of line with flag m) |
| $ | End of string (end of line with flag m) |
| \b | Word boundary — edge between \w and \W |
| \B | Not a word boundary |
Quantifiers
| * | 0 or more (greedy) |
| + | 1 or more (greedy) |
| ? | 0 or 1 — optional |
| {3} | Exactly 3 times |
| {2,} | 2 or more times |
| {2,5} | Between 2 and 5 times |
| *? +? ?? | Lazy versions — match as little as possible |
Groups & references
| (abc) | Capturing group — saved as $1, $2… |
| (?:abc) | Non-capturing group — grouping only |
| (?<name>abc) | Named capturing group |
| \1 | Backreference to group 1 |
| \k<name> | Backreference to a named group |
| a|b | Alternation — a OR b |
Lookarounds
| (?=abc) | Lookahead — followed by abc (abc not consumed) |
| (?!abc) | Negative lookahead — NOT followed by abc |
| (?<=abc) | Lookbehind — preceded by abc |
| (?<!abc) | Negative lookbehind — NOT preceded by abc |
Flags
| g | Global — find all matches, not just the first |
| i | Ignore case |
| m | Multiline — ^ and $ match line starts/ends |
| s | DotAll — . also matches newlines |
| u | Unicode — proper surrogate pairs and \p{…} |
| y | Sticky — match exactly at lastIndex |
The two mistakes everyone makes
- Greedy vs lazy.
<.+> on <a><b> matches the
whole thing; <.+?> matches <a> then <b>.
Greedy grabs as much as possible, lazy as little as possible.
- Catastrophic backtracking. Nested repetition like
(a+)+$ can take
exponential time on non-matching input and freeze your app. Our tester runs patterns in
a worker with a kill switch, so you can spot these safely before they reach production.