Regex Cheat Sheet: The Only Guide You'll Ever Need
A practical regex reference covering patterns, quantifiers, groups, lookaheads, and real-world examples. Bookmark this one.
C
Codeyaan
May 12, 2026
Regex Cheat Sheet: The Only Guide You'll Ever Need
Regular expressions are one of those tools that feel like dark magic until you learn the patterns. This cheat sheet covers everything you need for day-to-day development.
The Basics
| Pattern | Matches | Example |
|---|---|---|
. | Any character except newline | a.c → "abc", "a1c" |
\d | Any digit (0-9) | \d\d → "42" |
\w | Word character (a-z, A-Z, 0-9, _) | \w+ → "hello_world" |
\s | Whitespace (space, tab, newline) | \s+ → " " |
\D | NOT a digit | \D+ → "abc" |
\W | NOT a word character | \W → "@", "!" |
Quantifiers
| Pattern | Meaning |
|---|---|
* | 0 or more |
+ | 1 or more |
? | 0 or 1 (optional) |
{3} | Exactly 3 |
{2,5} | Between 2 and 5 |
{3,} | 3 or more |
Greedy vs Lazy: By default, quantifiers are greedy (match as much as possible). Add ? to make them lazy:
// Greedy: matches the longest possible string
"<div>hello</div>".match(/<.*>/); // "<div>hello</div>"
// Lazy: matches the shortest possible string
"<div>hello</div>".match(/<.*?>/); // "<div>"
Anchors
| Pattern | Meaning |
|---|---|
^ | Start of string (or line with m flag) |
$ | End of string (or line with m flag) |
\b | Word boundary |
/^\d+$/.test("12345"); // true — entire string is digits
/^\d+$/.test("123ab"); // false
Character Classes
[abc] // a, b, or c
[a-z] // any lowercase letter
[A-Z0-9] // uppercase letter or digit
[^abc] // NOT a, b, or c (negation)
Groups and Capturing
// Capturing group
const match = "2026-05-12".match(/(\d{4})-(\d{2})-(\d{2})/);
// match[1] = "2026", match[2] = "05", match[3] = "12"
// Named group
const match2 = "2026-05-12".match(/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/);
// match2.groups.year = "2026"
// Non-capturing group (just for grouping, no capture)
/(?:https?):\/\//
Real-World Patterns
Email validation (simple)
/[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}/
URL extraction
/https?:\/\/[\w.-]+(?:\/[\w./-]*)?/g
Phone number
/\+?\d[\d\s-]{7,}\d/
Hex color
/#[0-9a-fA-F]{3,8}/
IP address
/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/
Password strength (8+ chars, upper, lower, digit)
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/
Lookaheads and Lookbehinds
// 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/
JavaScript Regex Methods
// Test if pattern matches
/\d+/.test("hello123"); // true
// Find first match
"hello123".match(/\d+/); // ["123"]
// Find all matches
"a1b2c3".matchAll(/\d/g); // iterator of ["1"], ["2"], ["3"]
// Replace
"hello world".replace(/world/, "JS"); // "hello JS"
// Split
"a,b,,c".split(/,+/); // ["a", "b", "c"]
Common Mistakes
- Not escaping special characters:
.matches ANY character. Use\.for a literal dot. - Forgetting the
gflag: Without it, only the first match is returned. - Catastrophic backtracking: Nested quantifiers like
(a+)+can cause exponential processing time. - Over-engineering email validation: Use a simple pattern + send a verification email instead.
Practice
Test all these patterns live on our Regex Tester. It highlights matches in real-time and shows capture groups — perfect for learning and debugging.
Found this helpful? Share it:
C
Codeyaan
Writing about developer tools, web development, and making complex things simple.