RegexMay 15, 2026· 8 min read

Regex Cheat Sheet for JavaScript Developers

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

PatternMatches
.Any character except newline
\dAny digit (0–9)
\DAny non-digit
\wWord character (A–Z, a–z, 0–9, _)
\WNon-word character
\sWhitespace (space, tab, newline)
\SNon-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

PatternMeaning
*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

PatternMatches
^Start of string (or line with m flag)
$End of string (or line with m flag)
\bWord boundary
\BNon-word boundary
\AStart of string (not supported in JS — use ^)

Flags

FlagEffect
/gGlobal — find all matches, not just the first
/iCase-insensitive
/mMultiline — ^ and $ match start/end of each line
/sDot-all — . matches newlines too
/uUnicode — enables full Unicode matching
/ySticky — matches only from lastIndex position

Groups and Capturing

PatternMeaning
(abc)Capture group — captures "abc"
(?:abc)Non-capturing group — groups without capturing
(?<name>abc)Named capture group
\1Back-reference to group 1
a|bAlternation — 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.

← Back to Blog