Regex Cheat Sheet

Every regex token you keep re-searching, on one scrollable page.

Every regex token you look up weekly, on one page. All examples use the JavaScript flavor supported by our Regex Tester.

Character classes

PatternMeaning
\d \DDigit / non-digit
\w \WWord character [A-Za-z0-9_] / opposite
\s \SWhitespace / non-whitespace
.Any character (except newline, unless s flag)
[abc]Any of a, b, c
[^abc]Any character except a, b, c
[a-z]Character range, inclusive

Quantifiers

PatternMeaning
*0 or more (greedy)
+1 or more (greedy)
?0 or 1 (optional)
{3}Exactly 3
{2,5}Between 2 and 5
{2,}2 or more
*? +? ??Lazy versions — match as little as possible

Groups & lookaround

PatternMeaning
(...)Capturing group
(?:...)Non-capturing group
(?<name>...)Named capturing group
a|bAlternation — a or b
\1Backreference to group 1
(?=...)Lookahead
(?!...)Negative lookahead
(?<=...)Lookbehind
(?<!...)Negative lookbehind

Anchors & boundaries

PatternMeaning
^Start of string (or line with m flag)
$End of string (or line with m flag)
\bWord boundary
\BNot a word boundary

Flags

FlagMeaning
gGlobal — all matches
iCase-insensitive
mMultiline — ^ $ per line
sDot matches newline
uFull Unicode

Everyday recipes

TaskPattern
Email (loose)\b[\w.+-]+@[\w-]+\.[\w.]{2,}\b
URLhttps?://[^\s]+
IPv4\b(?:\d{1,3}\.){3}\d{1,3}\b
ISO date\d{4}-\d{2}-\d{2}
Hex color#[0-9a-fA-F]{3,6}\b
Double spaces {2,}

Frequently asked questions

Which regex flavor does this cheat sheet cover?

The JavaScript (ECMAScript) flavor — the same engine our Regex Tester uses and what you write in browser code and Node.js. It overlaps ~95% with PCRE, Python and Go regex for everyday patterns.

What is the difference between greedy and lazy quantifiers?

A greedy * matches as much as possible, then backtracks; a lazy *? matches as little as possible, then expands. Given hi, the greedy <.*> captures the whole string, the lazy <.*?> stops at the first >.

Do I need to escape special characters?

Yes: . ^ $ * + ? ( ) [ ] { } | \ all have meaning. Escape them with a backslash to match them literally. Inside character classes only ] \ ^ - need escaping.

Related tools

Copied