Regex
01 / 03

Regular Expressions

Regular Expressions

Syntax Reference

# Character classes
.        any character except newline
\d       digit [0-9]
\D       non-digit
\w       word char [a-zA-Z0-9_]
\W       non-word char
\s       whitespace (space, tab, newline)
\S       non-whitespace
[abc]    any of a, b, c
[^abc]   none of a, b, c
[a-z]    range a to z
[a-zA-Z0-9] alphanumeric

# Anchors
^        start of string (or line with m flag)
$        end of string (or line with m flag)
\b       word boundary  \bcat\b matches "cat" not "catch"
\B       non-word boundary

# Quantifiers
*        0 or more (greedy)
+        1 or more (greedy)
?        0 or 1
{n}      exactly n
{n,}     n or more
{n,m}    between n and m
*?  +?  ?? {n,m}?   non-greedy (lazy) versions

# Groups & alternation
(abc)    capturing group
(?:abc)  non-capturing group
(?<name>abc)  named capture group
a|b      a or b
\1       backreference to group 1

# Lookahead / Lookbehind
(?=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

# Flags (JavaScript)
g  global (find all matches)
i  case-insensitive
m  multiline (^ $ match line start/end)
s  dotAll (. matches newline)
u  unicode
d  indices (match.indices)

JavaScript Regex API

// Test — boolean
/^\d{4}$/.test('2024')            // true
/^\d{4}$/.test('abc')             // false

// Match — first match (or all with /g)
'hello world'.match(/\w+/)        // ['hello', index: 0, ...]
'hello world'.match(/\w+/g)       // ['hello', 'world']

// MatchAll — iterator of all matches with groups
const re = /(?<year>\d{4})-(?<month>\d{2})/g;
for (const m of '2024-01 and 2025-06'.matchAll(re)) {
  console.log(m.groups.year, m.groups.month);
}

// Replace
'foo bar'.replace(/\s+/, '-')     // 'foo-bar' (first only)
'foo bar baz'.replace(/\s+/g, '-')// 'foo-bar-baz' (all)
// With function
'hello'.replace(/(\w+)/, (_, w) => w.toUpperCase()) // 'HELLO'

// Split
'one, two,  three'.split(/,\s*/)  // ['one', 'two', 'three']

// Named capture groups
const m = '2024-03-07'.match(/(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})/);
const { y, m: month, d } = m.groups;  // '2024', '03', '07'

Common Patterns

// Email (simplified)
/^[^\s@]+@[^\s@]+\.[^\s@]+$/

// URL
/^https?:\/\/[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?$/

// IPv4
/^(\d{1,3}\.){3}\d{1,3}$/

// Phone (US)
/^\+?1?[-.\s]?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/

// 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-friendly)
/^[a-z0-9]+(?:-[a-z0-9]+)*$/

// Credit card (basic)
/^\d{4}[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{4}$/

// Password (min 8, uppercase, lowercase, digit, special)
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^a-zA-Z\d]).{8,}$/

// Strip HTML tags
str.replace(/<[^>]*>/g, '')

// Trim multiple spaces
str.replace(/\s+/g, ' ').trim()

// CamelCase to snake_case
str.replace(/([A-Z])/g, '_$1').toLowerCase()

// Extract all numbers
str.match(/-?\d+(\.\d+)?/g)

Keep your own version of these notes — editable, searchable, and organised by your stack.

Start free