Developer
Regular Expression Cheatsheet — Regex Quick Reference
Comprehensive regex cheatsheet with syntax, character classes, quantifiers, anchors, groups, lookahead/lookbehind, and common patterns.
Character Classes
| Pattern | Description | Example | Matches |
|---|---|---|---|
| . | Any character except newline | a.c | abc, a1c, a-c |
| \d | Digit (0-9) | \d{3} | 123, 456 |
| \D | Non-digit | \D+ | abc, --- |
| \w | Word character (a-z, A-Z, 0-9, _) | \w+ | hello_123 |
| \W | Non-word character | \W | !, @, # |
| \s | Whitespace (space, tab, newline) | a\sb | a b, a\tb |
| \S | Non-whitespace | \S+ | hello |
| [abc] | Any of a, b, or c | [aeiou] | a, e, i |
| [^abc] | Not a, b, or c | [^aeiou] | b, c, d |
| [a-z] | Range: a to z | [a-f] | a, b, c, d, e, f |
| [A-Z] | Range: A to Z | [A-F] | A, B, C, D, E, F |
| [0-9] | Range: 0 to 9 | [0-5] | 0, 1, 2, 3, 4, 5 |
Quantifiers
| Pattern | Description | Example | Matches |
|---|---|---|---|
| * | 0 or more | ab*c | ac, abc, abbc |
| + | 1 or more | ab+c | abc, abbc |
| ? | 0 or 1 | colou?r | color, colour |
| {n} | Exactly n | \d{4} | 2024 |
| {n,} | n or more | \d{2,} | 12, 123, 1234 |
| {n,m} | Between n and m | \d{2,4} | 12, 123, 1234 |
| *? | 0 or more (lazy) | <.*?> | <b> in <b>bold</b> |
| +? | 1 or more (lazy) | ".+?" | "hi" in "hi" "bye" |
Anchors & Boundaries
| Pattern | Description | Example | Matches |
|---|---|---|---|
| ^ | Start of string/line | ^Hello | Hello at start |
| $ | End of string/line | world$ | world at end |
| \b | Word boundary | \bcat\b | cat (not cats or concatenate) |
| \B | Non-word boundary | \Bcat | scat, concatenate |
Groups & References
| Pattern | Description | Example | Matches |
|---|---|---|---|
| (abc) | Capture group | (ha)+ | ha, haha, hahaha |
| (?:abc) | Non-capturing group | (?:ha)+ | Same as above, no capture |
| (?<name>abc) | Named capture group | (?<year>\d{4}) | Captures year |
| \1 | Back reference to group 1 | (\w)\1 | aa, bb, cc |
| a|b | Alternation (or) | cat|dog | cat or dog |
Lookahead & Lookbehind
| Pattern | Description | Example | Matches |
|---|---|---|---|
| (?=abc) | Positive lookahead | \d(?=px) | 3 in 3px |
| (?!abc) | Negative lookahead | \d(?!px) | 3 in 3em |
| (?<=abc) | Positive lookbehind | (?<=\$)\d+ | 100 in $100 |
| (?<!abc) | Negative lookbehind | (?<!\$)\d+ | 100 in EUR100 |
Flags / Modifiers
| Flag | Description | Example |
|---|---|---|
| g | Global search (find all matches) | /hello/g |
| i | Case-insensitive search | /hello/i matches Hello |
| m | Multi-line (^ and $ match line boundaries) | /^start/m |
| s | Dotall (. matches newlines) | /a.b/s matches a\nb |
| u | Unicode support | /\u{1F600}/u |
| y | Sticky (match from lastIndex) | /\d/y |
Common Patterns
| Pattern | Regex | Notes |
|---|---|---|
| ^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$ | Basic email validation | |
| URL | https?:\/\/[\w.-]+(?:\.[\w]{2,})[\/\w.-]* | HTTP/HTTPS URLs |
| IPv4 Address | \b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b | e.g., 192.168.1.1 |
| Hex Color | #[0-9a-fA-F]{3,8} | #fff, #FF5733, #FF573380 |
| Phone (US) | \(?\d{3}\)?[-\s]?\d{3}[-\s]?\d{4} | (555) 123-4567 |
| Date (YYYY-MM-DD) | \d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01]) | 2024-12-31 |
| Time (HH:MM) | (?:[01]\d|2[0-3]):[0-5]\d | 14:30, 09:15 |
| Username | ^[a-zA-Z][\w]{2,15}$ | 3-16 chars, starts with letter |
| Password (strong) | ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\w]).{8,}$ | Min 8, mixed case + digit + special |
| HTML Tag | <\/?[a-z][\w-]*[^>]*> | <div>, </span>, <img /> |