Regex Cheat Sheet: Complete Regular Expression Syntax Reference
Regular expressions are powerful and notoriously hard to remember. This regex cheat sheet covers every syntax category you'll actually use — anchors, character classes, quantifiers, groups, lookaheads, and common real-world patterns — in one place. Bookmark it, or just use an AI regex generator and skip the memorization entirely.
Anchors
Anchors don't match characters — they match positions in a string.
| Syntax | Meaning | Example |
|---|---|---|
| ^ | Start of string (or line in multiline mode) | ^Hello |
| $ | End of string (or line in multiline mode) | world$ |
| \b | Word boundary (between \w and \W) | \bcat\b |
| \B | Non-word boundary | \Bcat\B |
Character Classes
Character classes match a single character from a defined set.
| Syntax | Matches | Equivalent |
|---|---|---|
| . | Any character except newline | [^\n] |
| \d | Any digit | [0-9] |
| \D | Any non-digit | [^0-9] |
| \w | Word character | [a-zA-Z0-9_] |
| \W | Non-word character | [^a-zA-Z0-9_] |
| \s | Whitespace (space, tab, newline) | [ \t\n\r\f] |
| \S | Non-whitespace | [^ \t\n\r\f] |
| [abc] | One of: a, b, or c | — |
| [^abc] | Any character except a, b, or c | Negated class |
| [a-z] | Any lowercase letter | Range |
Quantifiers
Quantifiers specify how many times the preceding element must match. By default, they are greedy — they match as much as possible. Append ? to make them lazy (match as little as possible).
| Syntax | Meaning | Lazy version |
|---|---|---|
| * | 0 or more times | *? |
| + | 1 or more times | +? |
| ? | 0 or 1 time (optional) | ?? |
| {n} | Exactly n times | — |
| {n,} | n or more times | {n,}? |
| {n,m} | Between n and m times | {n,m}? |
Groups and Alternation
Groups allow you to apply quantifiers to multiple characters, capture substrings, and organize complex patterns.
| Syntax | Meaning |
|---|---|
| (abc) | Capturing group — matches and captures "abc" |
| (?:abc) | Non-capturing group — groups without capturing |
| (?<name>abc) | Named capturing group |
| a|b | Alternation — matches "a" or "b" |
| \1 | Backreference to first capturing group |
| \k<name> | Backreference to named group |
Named groups make complex patterns self-documenting:
// Named capture groups (JavaScript ES2018+)
const datePattern = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
const match = "2025-03-15".match(datePattern);
console.log(match.groups.year); // "2025"
console.log(match.groups.month); // "03"
console.log(match.groups.day); // "15"
// Non-capturing group (group without capturing)
const tagPattern = /(?:https?:\/\/)(\S+)/;
// Group 1 = domain+path, the protocol is grouped but NOT capturedLookaheads and Lookbehinds
Lookarounds let you match based on context without including that context in the match. They're zero-width — they don't consume characters.
// Positive lookahead: match 'foo' only if followed by 'bar' /foo(?=bar)/ // Negative lookahead: match 'foo' only if NOT followed by 'bar' /foo(?!bar)/ // Positive lookbehind: match 'bar' only if preceded by 'foo' /(?<=foo)bar/ // Negative lookbehind: match 'bar' only if NOT preceded by 'foo' /(?<!foo)bar/ // Practical: extract price without currency symbol const pricePattern = /(?<=\$)[\d.]+/; "Price: $19.99".match(pricePattern); // ["19.99"]
Flags (Modifiers)
| Flag | Name | Effect |
|---|---|---|
| g | Global | Find all matches (not just the first) |
| i | Case-insensitive | Match regardless of letter case |
| m | Multiline | ^ and $ match start/end of each line |
| s | Dotall | . matches newline characters too |
| u | Unicode | Enable full Unicode support |
| y | Sticky | Match only at the current position |
Common Regex Patterns You'll Actually Use
These are the patterns that come up over and over. Paste them directly or use them as a starting point:
// Email validation
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
// URL matching (http and https)
const urlRegex = /https?:\/\/[^\s/$.?#].[^\s]*/i;
// Phone number (US format: 555-123-4567 or (555) 123-4567)
const phoneRegex = /^(\(\d{3}\)|\d{3})[\s.-]\d{3}[\s.-]\d{4}$/;
// Date (YYYY-MM-DD)
const dateRegex = /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/;
// Strong password (min 8 chars, uppercase, lowercase, digit, special char)
const passwordRegex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/;
// IPv4 address
const ipv4Regex = /^(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)$/;
// Hex color code
const hexColorRegex = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;| Pattern | Use Case |
|---|---|
| /^[\w.-]+@[\w.-]+\.\w{2,}$/ | Email address (simple) |
| /https?:\/\/\S+/i | URL (basic) |
| /^\d{4}-\d{2}-\d{2}$/ | ISO date (YYYY-MM-DD) |
| /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/ | Hex color code |
| /^\d{1,3}(\.\d{1,3}){3}$/ | IPv4 address (basic) |
| /\b\d{5}(-\d{4})?\b/ | US ZIP code |
| /^[a-z][a-z0-9-]*[a-z0-9]$/i | Slug / URL-safe string |
| /^\+?[1-9]\d{1,14}$/ | E.164 international phone |
Why You Don't Need to Memorize All of This
Regex syntax is learnable, but it's also notoriously inconsistent across languages and engines. JavaScript's lookbehind support is ES2018+. Python uses re.VERBOSE for inline comments. PCRE has features that POSIX doesn't. Keeping all of this in your head while also remembering your actual codebase logic is a significant mental overhead.
AI regex generators change the calculus here. Instead of memorizing /(?<=[\$\€\£])[\d,]+(?:\.\d{2})?/, you just describe what you need:
"Match a price with a currency symbol (dollar, euro, or pound) followed by digits, optional thousands separators, and up to 2 decimal places."
The AI returns the pattern, explains each component, and flags any edge cases to be aware of. You understand it, you verify it, you use it — without spending 20 minutes on Stack Overflow.
When Cheat Sheets Still Matter
Even with AI tools available, understanding the underlying syntax makes you faster and more confident. You'll spot bugs in generated patterns. You'll know when a lookahead is the right tool. You'll understand why .* is greedy and why that causes the problem you're seeing. This cheat sheet is your foundation — AI handles the execution.
Need a regex pattern right now?
Describe what you want to match in plain English. RegSQL's AI generates the pattern, explains every part, and handles edge cases — for JavaScript, Python, Go, and more.
✦ Try RegSQL Regex Generator Free →The Bottom Line
Regular expressions are one of the most transferable skills in software development — they work everywhere from shell scripts to SQL to frontend validation. This regex cheat sheet covers the syntax you'll reach for most often. Use it as a reference, then use an AI generator when you need something more complex or specific. The best regex workflow is one that gets you to a working, understood pattern as fast as possible — however that happens.