Reference
Regex Cheat Sheet — Regular Expression Quick Reference
A complete regular expression cheat sheet covering anchors, character classes, quantifiers, groups, lookaheads, and common patterns. Every example links directly to the RegEx tester.
New to regex? Start with "What is Regex?" →Anchors
Character Classes
| Syntax | Description | Try it |
|---|---|---|
| . | Any character except newline | — |
| \d | Digit [0-9] | \d+ → |
| \D | Non-digit [^0-9] | — |
| \w | Word character [a-zA-Z0-9_] | \w+ → |
| \W | Non-word character | — |
| \s | Whitespace (space, tab, newline) | \s+ → |
| \S | Non-whitespace | — |
| [abc] | Any of a, b, or c | [aeiou] → |
| [^abc] | Anything except a, b, or c | [^aeiou] → |
| [a-z] | Character in range a to z | [a-zA-Z] → |
Quantifiers
Groups & References
| Syntax | Description | Try it |
|---|---|---|
| (abc) | Capturing group | (\d+)-(\d+) → |
| (?:abc) | Non-capturing group | (?:foo|bar) → |
| \1 | Backreference to group 1 | (\w+)\s\1 → |
| | | Alternation (OR) | cat|dog → |
Lookaheads & Lookbehinds
| Syntax | Description | Try it |
|---|---|---|
| (?=...) | Positive lookahead — followed by | \d(?= dollars) → |
| (?!...) | Negative lookahead — not followed by | \d(?! dollars) → |
| (?<=...) | Positive lookbehind — preceded by | (?<=\$)\d+ → |
| (?<!...) | Negative lookbehind — not preceded by | (?<!\$)\d+ → |
Flags
| Syntax | Description | Try it |
|---|---|---|
| g | Global — find all matches, not just the first | — |
| i | Case-insensitive match | — |
| m | Multiline — ^ and $ match start/end of each line | — |
| s | Dotall — . matches newline characters too | — |
Common Patterns
| Pattern | Regex | Try it |
|---|---|---|
| Email address | [a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,} | Try it → |
| URL (http/https) | https?:\/\/[\w\-.]+(:\d+)?(\/[\w\-._~:/?#[\]@!$&'()*+,;=%]*)? | Try it → |
| IPv4 address | \b(\d{1,3}\.){3}\d{1,3}\b | Try it → |
| Date (YYYY-MM-DD) | \d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01]) | Try it → |
| Time (HH:MM) | (?:[01]\d|2[0-3]):[0-5]\d | Try it → |
| Hex colour | #(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})\b | Try it → |
| UK postcode | [A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2} | Try it → |
| US ZIP code | \b\d{5}(?:-\d{4})?\b | Try it → |
| Phone (intl, loose) | \+?[\d\s\-().]{7,20} | Try it → |
| Digits only | ^\d+$ | Try it → |
| Alphanumeric only | ^[a-zA-Z0-9]+$ | Try it → |
| Slug (URL-safe) | ^[a-z0-9]+(?:-[a-z0-9]+)*$ | Try it → |
| HTML tag | <([a-zA-Z][a-zA-Z0-9]*)\b[^>]*>.*?<\/\1> | Try it → |
| Repeated word | \b(\w+)\s+\1\b | Try it → |
| Empty lines | ^\s*$ | Try it → |
Test any regex pattern live in your browser
Paste a pattern, enter test text, and see matches highlighted instantly
Open RegEx Tester →