Regex Cheat Sheet
Every token, quantifier, and assertion you'll need — copyable and printable.
Anchors
| Token | Meaning | Example | |
|---|---|---|---|
| ^ | Start of string (or line, with the m flag) | ^Hello | |
| $ | End of string (or line, with the m flag) | world$ | |
| \b | Word boundary | \bcat\b | |
| \B | Not a word boundary | \Bcat\B |
Character Classes
| Token | Meaning | Example | |
|---|---|---|---|
| . | Any character except line breaks | a.c | |
| \d | Any digit (0-9) | \d{3} | |
| \D | Any non-digit | \D+ | |
| \w | Any word character (letter, digit, underscore) | \w+ | |
| \W | Any non-word character | \W | |
| \s | Any whitespace character | \s+ | |
| \S | Any non-whitespace character | \S+ | |
| [abc] | Any of a, b, or c | [abc] | |
| [^abc] | Any character except a, b, or c | [^abc] | |
| [a-z] | Any character in the range a to z | [a-z]+ |
Quantifiers
| Token | Meaning | Example | |
|---|---|---|---|
| * | Zero or more (greedy) | ab* | |
| + | One or more (greedy) | ab+ | |
| ? | Zero or one (optional) | colou?r | |
| {n} | Exactly n times | \d{4} | |
| {n,} | n or more times | \d{2,} | |
| {n,m} | Between n and m times | \d{2,4} | |
| *? | Zero or more (lazy) | <.*?> | |
| +? | One or more (lazy) | ".+?" |
Groups & Backreferences
| Token | Meaning | Example | |
|---|---|---|---|
| (...) | Capturing group | (ab)+ | |
| (?:...) | Non-capturing group | (?:ab)+ | |
| (?<name>...) | Named capturing group | (?<year>\d{4}) | |
| \1 | Backreference to group 1 | (\w)\1 | |
| \k<name> | Backreference to a named group | (?<q>['"])\k<q> | |
| | | Alternation (OR) | cat|dog |
Lookaround Assertions
| Token | Meaning | Example | |
|---|---|---|---|
| (?=...) | Positive lookahead | \d+(?=px) | |
| (?!...) | Negative lookahead | \d+(?!px) | |
| (?<=...) | Positive lookbehind | (?<=\$)\d+ | |
| (?<!...) | Negative lookbehind | (?<!\$)\d+ |
Escaping
| Token | Meaning | Example | |
|---|---|---|---|
| \. | Literal dot | 3\.14 | |
| \\ | Literal backslash | C:\\\\Users | |
| \/ | Literal forward slash | https:\/\/ | |
| \( | Literal opening parenthesis | \(\d{3}\) | |
| \[ | Literal opening bracket | \[note\] |
Unicode
| Token | Meaning | Example | |
|---|---|---|---|
| \p{L} | Any Unicode letter (needs u flag) | \p{L}+ | |
| \p{N} | Any Unicode number (needs u flag) | \p{N}+ | |
| \p{Emoji} | Any emoji character (needs u/v flag) | \p{Emoji} | |
| \u{1F600} | Unicode code point escape (needs u flag) | \u{1F600} |
Flags
| Token | Meaning | Example | |
|---|---|---|---|
| g | Global — find all matches, not just the first | — | |
| i | Case-insensitive matching | — | |
| m | Multiline — ^ and $ match line boundaries | — | |
| s | Dot-all — . also matches newlines | — | |
| u | Unicode — enables full Unicode matching | — | |
| y | Sticky — matches only from lastIndex | — |