📚
Regex Cheatsheet
Regex & DebuggingInteractive, searchable regex reference covering character classes, quantifiers, anchors, groups, lookaheads, and flags.
Character Classes
| Pattern | Description | Example |
|---|
| . | Any character except newline | a.c → abc, a1c |
| \d | Digit [0-9] | \d+ → 123, 42 |
| \D | Non-digit [^0-9] | \D+ → abc, !@# |
| \w | Word character [a-zA-Z0-9_] | \w+ → hello_42 |
| \W | Non-word character | \W+ → @#!, ... |
| \s | Whitespace (space, tab, newline) | a\sb → a b |
| \S | Non-whitespace | \S+ → hello |
| [abc] | Character set — matches a, b, or c | [aeiou] → vowels |
| [^abc] | Negated set — anything except a, b, c | [^0-9] → non-digits |
| [a-z] | Range — lowercase letters | [a-zA-Z] → letters |
Quantifiers
| Pattern | Description | Example |
|---|
| * | 0 or more (greedy) | ab*c → ac, abc, abbc |
| + | 1 or more (greedy) | ab+c → abc, abbc |
| ? | 0 or 1 (optional) | colou?r → color, colour |
| {n} | Exactly n times | \d{3} → 123 |
| {n,} | n or more times | \d{2,} → 12, 123, 1234 |
| {n,m} | Between n and m times | \d{2,4} → 12, 123, 1234 |
| *? | 0 or more (lazy/non-greedy) | <.*?> → first tag only |
| +? | 1 or more (lazy/non-greedy) | ".+?" → first quoted |
| ?? | 0 or 1 (lazy) | colou??r → prefers color |
Anchors
| Pattern | Description | Example |
|---|
| ^ | Start of string (or line with m flag) | ^Hello → starts with Hello |
| $ | End of string (or line with m flag) | world$ → ends with world |
| \b | Word boundary | \bword\b → whole word |
| \B | Non-word boundary | \Bword → mid-word only |
Groups & References
| Pattern | Description | Example |
|---|
| (abc) | Capturing group | (ha)+ → haha |
| (?:abc) | Non-capturing group | (?:ha)+ → haha |
| (?<name>abc) | Named capturing group | (?<year>\d{4}) |
| \1 | Backreference to group 1 | (\w+)\s\1 → word word |
| \k<name> | Named backreference | \k<year> |
| a|b | Alternation (or) | cat|dog → cat or dog |
Lookaheads & Lookbehinds
| Pattern | Description | Example |
|---|
| (?=abc) | Positive lookahead | \d(?=px) → 3 in 3px |
| (?!abc) | Negative lookahead | \d(?!px) → 3 not before px |
| (?<=abc) | Positive lookbehind | (?<=\$)\d+ → 99 in $99 |
| (?<!abc) | Negative lookbehind | (?<!\$)\d+ → not after $ |
Flags
| Pattern | Description | Example |
|---|
| g | Global — find all matches, not just first | /a/g → all a's |
| i | Case-insensitive matching | /hello/i → Hello, HELLO |
| m | Multiline — ^ and $ match line boundaries | /^line/m → each line |
| s | DotAll — . also matches newline | /a.b/s → a\nb |
| u | Unicode — full Unicode matching | /\u{1F600}/u → 😀 |
| y | Sticky — match at exact position | lastIndex anchored |