Regex Cheat Sheet
Regular expression syntax at a glance.
A quick reference to the most-used regular-expression syntax. Try any pattern in the Regex Explainer to see it broken down token by token.
Anchors & boundaries
| Token | Matches |
|---|---|
| ^ | Start of the string (or line with the m flag) |
| $ | End of the string (or line with the m flag) |
| \b | A word boundary |
| \B | A non-word boundary |
Character classes
| Token | Matches |
|---|---|
| . | Any character except newline |
| \d / \D | A digit / a non-digit |
| \w / \W | A word character (letter, digit, _) / a non-word character |
| \s / \S | Whitespace / non-whitespace |
| [abc] | Any one of a, b or c |
| [^abc] | Any character except a, b or c |
| [a-z] | Any character in the range a to z |
Quantifiers
| Token | Matches |
|---|---|
| * | Zero or more |
| + | One or more |
| ? | Zero or one (optional) |
| {n} | Exactly n times |
| {n,} | n or more times |
| {n,m} | Between n and m times |
| *? +? ?? | Lazy (as few as possible) versions |
Groups & lookaround
| Token | Matches |
|---|---|
| (abc) | A capturing group |
| (?:abc) | A non-capturing group |
| (?<name>abc) | A named capturing group |
| a|b | a or b (alternation) |
| (?=abc) | Lookahead — followed by abc |
| (?!abc) | Negative lookahead — not followed by abc |
| (?<=abc) | Lookbehind — preceded by abc |
| (?<!abc) | Negative lookbehind — not preceded by abc |
Flags
| Flag | Effect |
|---|---|
| g | Global — find all matches, not just the first |
| i | Case-insensitive |
| m | Multiline — ^ and $ match line starts/ends |
| s | Dotall — . also matches newlines |
| u | Unicode |
| y | Sticky — match from lastIndex only |
Common patterns
| Goal | Pattern |
|---|---|
| Email (simple) | ^[^\s@]+@[^\s@]+\.[^\s@]+$ |
| URL | https?://[^\s]+ |
| Digits only | ^\d+$ |
| Hex color | #(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6}) |
| ZIP / postcode (US) | ^\d{5}(-\d{4})?$ |
FAQ
Escape it with a backslash: \. — an unescaped dot matches any character.
It matches as few characters as possible. For example, <.*?> stops at the first > instead of the last.
Last reviewed 2026-08-20.