Regex Cheat Sheet
Every regex token you keep re-searching, on one scrollable page.
Every regex token you look up weekly, on one page. All examples use the JavaScript flavor supported by our Regex Tester.
Character classes
| Pattern | Meaning |
|---|---|
| \d \D | Digit / non-digit |
| \w \W | Word character [A-Za-z0-9_] / opposite |
| \s \S | Whitespace / non-whitespace |
| . | Any character (except newline, unless s flag) |
| [abc] | Any of a, b, c |
| [^abc] | Any character except a, b, c |
| [a-z] | Character range, inclusive |
Quantifiers
| Pattern | Meaning |
|---|---|
| * | 0 or more (greedy) |
| + | 1 or more (greedy) |
| ? | 0 or 1 (optional) |
| {3} | Exactly 3 |
| {2,5} | Between 2 and 5 |
| {2,} | 2 or more |
| *? +? ?? | Lazy versions — match as little as possible |
Groups & lookaround
| Pattern | Meaning |
|---|---|
| (...) | Capturing group |
| (?:...) | Non-capturing group |
| (?<name>...) | Named capturing group |
| a|b | Alternation — a or b |
| \1 | Backreference to group 1 |
| (?=...) | Lookahead |
| (?!...) | Negative lookahead |
| (?<=...) | Lookbehind |
| (?<!...) | Negative lookbehind |
Anchors & boundaries
| Pattern | Meaning |
|---|---|
| ^ | Start of string (or line with m flag) |
| $ | End of string (or line with m flag) |
| \b | Word boundary |
| \B | Not a word boundary |
Flags
| Flag | Meaning |
|---|---|
| g | Global — all matches |
| i | Case-insensitive |
| m | Multiline — ^ $ per line |
| s | Dot matches newline |
| u | Full Unicode |
Everyday recipes
| Task | Pattern |
|---|---|
| Email (loose) | \b[\w.+-]+@[\w-]+\.[\w.]{2,}\b |
| URL | https?://[^\s]+ |
| IPv4 | \b(?:\d{1,3}\.){3}\d{1,3}\b |
| ISO date | \d{4}-\d{2}-\d{2} |
| Hex color | #[0-9a-fA-F]{3,6}\b |
| Double spaces | {2,} |
Frequently asked questions
Which regex flavor does this cheat sheet cover?
The JavaScript (ECMAScript) flavor — the same engine our Regex Tester uses and what you write in browser code and Node.js. It overlaps ~95% with PCRE, Python and Go regex for everyday patterns.
What is the difference between greedy and lazy quantifiers?
A greedy * matches as much as possible, then backtracks; a lazy *? matches as little as possible, then expands. Given hi, the greedy <.*> captures the whole string, the lazy <.*?> stops at the first >.
Do I need to escape special characters?
Yes: . ^ $ * + ? ( ) [ ] { } | \ all have meaning. Escape them with a backslash to match them literally. Inside character classes only ] \ ^ - need escaping.