Regex Cheatsheet

A quick reference for regular expressions (JavaScript / PCRE-compatible syntax). Try any of these live in the regex tester — it highlights matches and shows your capture groups.

Open the regex tester →

Character classes

TokenMatches
.Any character except newline (with s flag, includes newline)
\d / \DA digit [0-9] / a non-digit
\w / \WWord char [A-Za-z0-9_] / non-word char
\s / \SWhitespace / non-whitespace
[abc]Any one of a, b, or c
[^abc]Any character except a, b, c
[a-z0-9]A range: any lowercase letter or digit

Anchors & boundaries

TokenMatches
^ / $Start / end of string (or line, with m flag)
\b / \BWord boundary / non-boundary

Quantifiers

TokenMatches
*0 or more
+1 or more
?0 or 1 (optional)
{3}Exactly 3
{2,}2 or more
{2,5}Between 2 and 5
*? +? ??Lazy (as few as possible) versions of the above

Groups & alternation

TokenMeaning
(abc)Capture group (numbered)
(?:abc)Non-capturing group
(?<name>abc)Named capture group
a|bMatch a or b
\1Backreference to group 1

Lookarounds

TokenMeaning
(?=abc)Lookahead: followed by abc
(?!abc)Negative lookahead: not followed by abc
(?<=abc)Lookbehind: preceded by abc
(?<!abc)Negative lookbehind: not preceded by abc

Flags

FlagEffect
gGlobal — find all matches, not just the first
iCase-insensitive
mMultiline — ^/$ match line starts/ends
sDotall — . matches newlines
uUnicode mode
ySticky — match from lastIndex only

Common patterns

Starting points — validate against your real data; "perfect" email/URL regexes don't exist.

GoalPattern
Integer-?\d+
Decimal-?\d+(\.\d+)?
Email (pragmatic)[^\s@]+@[^\s@]+\.[^\s@]+
URL (http/https)https?:\/\/[^\s]+
Hex color#[0-9a-fA-F]{6}\b
IPv4 (loose)\b\d{1,3}(\.\d{1,3}){3}\b
ISO date\d{4}-\d{2}-\d{2}
Slug[a-z0-9]+(?:-[a-z0-9]+)*
Test these in the live regex tester →