Regex Cheat Sheet

Regular expression syntax at a glance.

A quick reference to the most-used regular-expression syntax. Try any pattern in the Regex Explainer to see it broken down token by token.

Anchors & boundaries

TokenMatches
^Start of the string (or line with the m flag)
$End of the string (or line with the m flag)
\bA word boundary
\BA non-word boundary

Character classes

TokenMatches
.Any character except newline
\d / \DA digit / a non-digit
\w / \WA word character (letter, digit, _) / a non-word character
\s / \SWhitespace / non-whitespace
[abc]Any one of a, b or c
[^abc]Any character except a, b or c
[a-z]Any character in the range a to z

Quantifiers

TokenMatches
*Zero or more
+One or more
?Zero or one (optional)
{n}Exactly n times
{n,}n or more times
{n,m}Between n and m times
*? +? ??Lazy (as few as possible) versions

Groups & lookaround

TokenMatches
(abc)A capturing group
(?:abc)A non-capturing group
(?<name>abc)A named capturing group
a|ba or b (alternation)
(?=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 — ^ and $ match line starts/ends
sDotall — . also matches newlines
uUnicode
ySticky — match from lastIndex only

Common patterns

GoalPattern
Email (simple)^[^\s@]+@[^\s@]+\.[^\s@]+$
URLhttps?://[^\s]+
Digits only^\d+$
Hex color#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})
ZIP / postcode (US)^\d{5}(-\d{4})?$

FAQ

Escape it with a backslash: \. — an unescaped dot matches any character.

It matches as few characters as possible. For example, <.*?> stops at the first > instead of the last.

Last reviewed 2026-08-20.