Cheat sheetDev

Regex syntax

Every regex token you'll actually use, translated to one plain-English line — classes, quantifiers, anchors, groups and flags in one printable sheet.

Last updated

Character classes

TokenMeaning
.Any single character except a newline (the s flag adds newlines).
\d \DA digit 0–9 / anything that isn't one.
\w \WA word character (letter, digit, underscore) / anything else.
\s \SAny whitespace (space, tab, newline) / anything that isn't.
[abc]Exactly one character from the set: a, b or c.
[^abc]One character that is NOT a, b or c — ^ negates only inside brackets.
[a-z0-9]One character from either range; ranges and singles mix freely.

Quantifiers

TokenMeaning
*The previous item, zero or more times.
+The previous item, one or more times.
?The previous item, optional (zero or one).
{3} {2,5} {2,}Exactly 3 / between 2 and 5 / at least 2 repetitions.
*? +? ??The lazy versions: match as LITTLE as possible instead of as much.

Anchors & boundaries

TokenMeaning
^ $Start / end of the string (of each line, with the m flag).
\bA word boundary — the zero-width seam between a word character and a non-word one.
\BThe opposite: a position that is NOT a word boundary.

Groups & alternation

TokenMeaning
(cat|dog)Either alternative, captured as group 1.
(?:cat|dog)Same choice, but not captured — cheaper and keeps numbering clean.
(?<name>…)A named capture, read back as \k<name> or in match.groups.name.
\1Whatever group 1 actually matched, repeated here (a backreference).
(?=…) (?!…)Lookahead: the next text must / must not match — without consuming it.
(?<=…) (?<!…)Lookbehind: the preceding text must / must not match.

Flags

TokenMeaning
gGlobal — find every match, not just the first.
iCase-insensitive matching.
mMultiline — ^ and $ also match at line breaks.
sDotall — . matches newlines too.
uUnicode mode — treat the pattern as code points; needed for \p{…}.

Worth remembering

  • To match a special character literally, escape it: \. \+ \( \[ — forgetting this on the dot is the most common regex bug in the wild.
  • Every token here is testable live in the Regex Tester — paste a pattern and your own text, nothing leaves the browser.

Related in Cheat sheets