Regex Cheatsheet — Complete Regular Expressions Reference
A complete, printable regex reference guide. Covers anchors, character classes, quantifiers, groups, lookaheads, and more. Works for JavaScript, Python, and most regex engines.
regex cheatsheet
regular expression reference
regex syntax guide
regex quick reference
regex symbols list
Anchors
| Syntax / Pattern | Meaning | Example / Notes |
|---|---|---|
| ^ | Start of string (or line with m flag) | /^hello/ matches "hello world" but not "say hello" |
| $ | End of string (or line with m flag) | /world$/ matches "hello world" but not "world hello" |
| \b | Word boundary | /\bcat\b/ matches "cat" but not "catch" |
| \B | Not a word boundary | /\Bcat/ matches "concatenate" but not "cat" |
Character Classes
| Syntax / Pattern | Meaning | Example / Notes |
|---|---|---|
| . | Any character except newline | With the s (dotAll) flag, also matches newlines |
| \d | Digit [0-9] | \D matches non-digit |
| \w | Word character [a-zA-Z0-9_] | \W matches non-word character |
| \s | Whitespace (space, tab, newline) | \S matches non-whitespace |
| [abc] | Character set: a, b, or c | [^abc] negates — matches anything except a, b, c |
| [a-z] | Range: any lowercase letter | [a-zA-Z0-9] alphanumeric |
Quantifiers
| Syntax / Pattern | Meaning | Example / Notes |
|---|---|---|
| * | Zero or more (greedy) | a* matches "", "a", "aaa" |
| + | One or more (greedy) | a+ matches "a", "aaa" but not "" |
| ? | Zero or one (optional) | colou?r matches "color" and "colour" |
| {n} | Exactly n times | \d{4} matches exactly 4 digits |
| {n,} | n or more times | \d{2,} matches 2+ digits |
| {n,m} | Between n and m times | \d{2,4} matches 2, 3, or 4 digits |
| *? +? ?? | Lazy (non-greedy) versions | <.+?> matches the shortest possible tag |
Groups & References
| Syntax / Pattern | Meaning | Example / Notes |
|---|---|---|
| (abc) | Capturing group | Captured in $1, $2... or match[1], match[2] |
| (?:abc) | Non-capturing group | Groups without capturing — better for performance |
| (?<name>abc) | Named capture group (JS/Python) | Access via match.groups.name or (?P<name>...) in Python |
| \1 | Backreference to group 1 | (\w+) \1 matches repeated words like "hello hello" |
Lookaheads & Lookbehinds
| Syntax / Pattern | Meaning | Example / Notes |
|---|---|---|
| (?=abc) | Positive lookahead — followed by abc | \d+(?= dollars) matches numbers before " dollars" |
| (?!abc) | Negative lookahead — NOT followed by abc | \d+(?! dollars) matches numbers not before " dollars" |
| (?<=abc) | Positive lookbehind — preceded by abc | (?<=\$)\d+ matches digits after a dollar sign |
| (?<!abc) | Negative lookbehind | Not supported in all engines |
Flags
| Syntax / Pattern | Meaning | Example / Notes |
|---|---|---|
| g | Global — find all matches | Without g, only the first match is returned |
| i | Case-insensitive | /hello/i matches "Hello", "HELLO", "hello" |
| m | Multiline — ^ and $ match line start/end | Affects behavior of ^ and $ anchors |
| s | DotAll — . matches newlines too | ES2018+ / Python re.DOTALL |
| u | Unicode mode | Required for Unicode property escapes like \p{L} |
| d | Indices — include match start/end positions | ES2022+ only |
Common Patterns
| Syntax / Pattern | Meaning | Example / Notes |
|---|---|---|
| /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/ | ||
| URL | /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z]{2,6}/ | |
| IP Address | /\b(?:\d{1,3}\.){3}\d{1,3}\b/ | |
| US Phone | /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/ | |
| Date YYYY-MM-DD | /^\d{4}-\d{2}-\d{2}$/ | |
| Hex Color | /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/ | |
| Slug | /^[a-z0-9]+(?:-[a-z0-9]+)*$/ | |
| Password (8+ chars, 1 letter, 1 digit) | /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/ |
Frequently Asked Questions
What is the difference between * and + in regex?
`*` means zero or more occurrences — the match succeeds even if the character is absent. `+` means one or more — at least one occurrence is required.
How do I make a regex case-insensitive?
Add the `i` flag: `/pattern/i` in JavaScript, or `re.compile("pattern", re.IGNORECASE)` in Python.
What does \b do in regex?
`\b` is a word boundary assertion. It matches the position between a word character (\w) and a non-word character. `/\bcat\b/` matches "cat" in "the cat sat" but not in "catch".
Ready to put this to use?
Test patterns interactively in our live tool.