Regular expressions look like line noise until you understand their grammar. Every character in a regex has a specific meaning — and once you learn the building blocks, you can read and write complex patterns confidently. This guide teaches every concept with visual breakdowns and ends with 20 real-world patterns you can use today.
⚡ TL;DR: . matches any char. \d digit, \w word char, \s space. [] character class. + one or more, * zero or more, ? optional. ^ start, $ end. () capture group. (?:) non-capturing group. (?=) lookahead. | alternation. Flags: g global, i case-insensitive, m multiline.
Building blocks — character classes and quantifiers
// Character classes:
/\d/ // Digit: [0-9]
/\D/ // Non-digit
/\w/ // Word char: [a-zA-Z0-9_]
/\W/ // Non-word
/\s/ // Whitespace: space, tab, newline
/\S/ // Non-whitespace
/./ // Any char except newline
/[aeiou]/ // Character set: any vowel
/[^aeiou]/ // Negated set: any non-vowel
/[a-z]/ // Range: lowercase a-z
// Quantifiers:
/a+/ // One or more a
/a*/ // Zero or more a
/a?/ // Zero or one a (optional)
/a{3}/ // Exactly 3 a's
/a{2,5}/ // Between 2 and 5 a's
/a{2,}/ // Two or more a's
// Greedy vs lazy:
/<.+>/ // Greedy: matches longest possible
/<.+?>/ // Lazy: matches shortest possible
''.match(/<.+>/)[0] // '' (greedy)
''.match(/<.+?>/)[0] // '' (lazy)
Anchors, groups, and alternation
// Anchors:
/^hello/ // Matches 'hello' at START of string
/world$/ // Matches 'world' at END of string
/\bhello\b/ // Word boundary: 'hello' not 'helloworld'
// Groups:
/(abc)+/ // Capture group: abc one or more times
/(?:abc)/ // Non-capturing group (no capture, for grouping only)
// Named groups (most readable):
const dateRegex = /(?\d{4})-(?\d{2})-(?\d{2})/;
const match = '2026-04-07'.match(dateRegex);
const { year, month, day } = match.groups; // year='2026', month='04', day='07'
// Alternation:
/cat|dog/ // Matches 'cat' or 'dog'
/^(GET|POST|PUT|PATCH|DELETE)$/ // HTTP methods
// Backreferences:
/(\w+) \1/ // Matches repeated word: 'hello hello'
Lookahead and lookbehind
// Lookahead: match A only if followed by B
/foo(?=bar)/ // Positive: 'foo' only if followed by 'bar'
/foo(?!bar)/ // Negative: 'foo' only if NOT followed by 'bar'
// Lookbehind: match A only if preceded by B
/(?<=\$)\d+/ // Positive: digits preceded by $
/(?
20 real-world patterns
// Email (simplified but practical):
/^[\w.+\-]+@[\w\-]+\.[a-zA-Z]{2,}$/
// URL:
/^https?:\/\/([\w\-]+\.)+[\w\-]+(\/[^\s]*)?$/
// IPv4:
/^(\d{1,3}\.){3}\d{1,3}$/
// Strong password (8+ chars, upper, lower, digit, special):
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/
// UUID v4:
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
// ISO 8601 date:
/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$/
// Hex color:
/^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/
// Remove HTML tags:
const stripped = html.replace(/<[^>]*>/g, '');
// Extract all links from HTML:
const links = html.match(/href=["']([^"']+)["']/g);
- ✅ Named groups (?
...) for readable captures - ✅ Non-capturing groups (?:...) when you group but do not capture
- ✅ Lookahead/lookbehind for context-aware matching without consuming chars
- ✅ Lazy quantifiers .+? for shortest match
- ✅ Test at regex101.com — shows match groups visually with explanation
- ❌ Never write one-line regex for complex validation — break into steps
- ❌ Never use user-supplied regex without safe-regex check — ReDoS risk
Regex performance matters — see the Node.js security guide for ReDoS (catastrophic backtracking) prevention. External reference: regex101 — interactive regex tester.
Recommended Reading
→ Designing Data-Intensive Applications — The essential book every senior developer needs.
→ The Pragmatic Programmer — Timeless engineering wisdom for writing better code.
Affiliate links. We earn a small commission at no extra cost to you.
Free Weekly Newsletter
🚀 Don’t Miss the Next Cheat Code
Join 1,000+ senior developers getting expert JS, Python, AWS and system design secrets weekly.
Discover more from CheatCoders
Subscribe to get the latest posts sent to your email.
