Regular expressions are one of those skills that pay off indefinitely. Once you understand the patterns, you can validate inputs, extract data, and transform text in a single line of code. This cheat sheet covers everything you need for real-world JavaScript regex.
Character Classes
| Pattern | Matches |
|---|
| . | Any character except newline |
| \d | Any digit (0–9) |
| \D | Any non-digit |
| \w | Word character (A–Z, a–z, 0–9, _) |
| \W | Non-word character |
| \s | Whitespace (space, tab, newline) |
| \S | Non-whitespace |
| [abc] | Any of a, b, or c |
| [^abc] | Anything except a, b, or c |
| [a-z] | Any lowercase letter |
| [A-Z0-9] | Any uppercase letter or digit |
Quantifiers
| Pattern | Meaning |
|---|
| * | 0 or more (greedy) |
| + | 1 or more (greedy) |
| ? | 0 or 1 (optional) |
| {3} | Exactly 3 |
| {3,} | 3 or more |
| {3,6} | Between 3 and 6 |
| *? | 0 or more (lazy — as few as possible) |
| +? | 1 or more (lazy) |
Anchors
| Pattern | Matches |
|---|
| ^ | Start of string (or line with m flag) |
| $ | End of string (or line with m flag) |
| \b | Word boundary |
| \B | Non-word boundary |
| \A | Start of string (not supported in JS — use ^) |
Flags
| Flag | Effect |
|---|
| /g | Global — find all matches, not just the first |
| /i | Case-insensitive |
| /m | Multiline — ^ and $ match start/end of each line |
| /s | Dot-all — . matches newlines too |
| /u | Unicode — enables full Unicode matching |
| /y | Sticky — matches only from lastIndex position |
Groups and Capturing
| Pattern | Meaning |
|---|
| (abc) | Capture group — captures "abc" |
| (?:abc) | Non-capturing group — groups without capturing |
| (?<name>abc) | Named capture group |
| \1 | Back-reference to group 1 |
| a|b | Alternation — matches a or b |
| (?=abc) | Positive lookahead — followed by abc |
| (?!abc) | Negative lookahead — not followed by abc |
| (?<=abc) | Positive lookbehind — preceded by abc |
| (?<!abc) | Negative lookbehind — not preceded by abc |
Common Patterns
// Email address
/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
// URL
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&/=]*)/
// Phone number (US)
/^\+?1?[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/
// IPv4 address
/^(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?)$/
// Date (YYYY-MM-DD)
/^\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})$/
// Slug (URL-safe)
/^[a-z0-9]+(?:-[a-z0-9]+)*$/
// Strong password (8+ chars, upper, lower, digit, special)
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/JavaScript Regex Methods
const str = "Hello World 123";
const pattern = /\d+/g;
// Test — returns boolean
pattern.test(str); // true
// Match — returns array of matches
str.match(pattern); // ["123"]
str.match(/\w+/g); // ["Hello", "World", "123"]
// Replace
str.replace(/World/, "JS"); // "Hello JS 123"
str.replace(/\d/g, "#"); // "Hello World ###"
// Replace with function
str.replace(/(\w+)/g, (match) => match.toUpperCase());
// Split
"a,b,,c".split(/,+/); // ["a", "b", "c"]
// Named groups
const date = "2026-05-15";
const { year, month, day } = date.match(
/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/
).groups;Try it now: Regex Tester — test any pattern with live match highlighting, group captures, and replace mode.