Regex Cheat Sheet: Regular Expression Syntax With Examples
A practical regex cheat sheet: character classes, anchors, quantifiers, groups, lookarounds, flags and replacement references, each with a working JavaScript example.
Characters and character classes
Most characters match themselves: cat matches the text cat. The characters . * + ? ^ $ { } ( ) [ ] | \ and / have special meanings, so put a backslash before them to match them literally, as in \. for a dot.
A character class matches one character from a set. Square brackets list the set, a hyphen makes a range and a leading ^ negates it. Shorthand classes cover the most common sets.
. any character except a line break (any at all with the s flag)
\d digit 0-9 \D not a digit
\w letter, digit or _ \W not a word character
\s space, tab or line break \S not whitespace
[abc] a, b or c [^abc] anything else
[a-z0-9] a lowercase letter or digit
\p{L} any Unicode letter (needs the u or v flag)Anchors and boundaries
Anchors match a position rather than a character. Use ^ and $ to require that the whole value matches, which is essential for validation: without them, \d{3} also matches the middle of 12345.
With the m (multiline) flag, ^ and $ match at the start and end of every line, which is what you want when testing one value per line.
^\d{3}$ exactly three digits and nothing else
\bcat\b the word cat, not catalog or bobcat
^#.*$ (m) every line that starts with #Quantifiers: how many times
A quantifier repeats the item just before it. Quantifiers are greedy by default and take as much as they can; add ? to make one lazy so it takes as little as possible.
To repeat more than one character, wrap it in a group first: (ab)+ repeats ab, while ab+ repeats only the b.
a* zero or more a+ one or more a? optional
a{3} exactly 3 a{2,} 2 or more a{2,5} 2 to 5
<.+> greedy: all of <b>bold</b>
<.+?> lazy: just <b>Groups, alternation and backreferences
Parentheses group parts of a pattern and capture what they match, numbered from 1 in the order of their opening parenthesis. Name a group with (?<name>…) to make code easier to read, or use (?:…) to group without capturing.
A vertical bar separates alternatives, and a backreference matches the exact text a group captured earlier, which is how you find repeated words.
(\d{4})-(\d{2}) groups 1 and 2
(?<year>\d{4}) named group year
(?:jpg|png|gif) one of three, not captured
\b(\w+)\s+\1\b a repeated word, such as "the the"Lookahead and lookbehind
Lookarounds check what comes before or after the current position without including it in the match. They are ideal for rules such as "a number followed by px" or "a password containing a digit".
\d+(?=px) digits followed by px (px not included)
\d+(?!px) digits not followed by px
(?<=\$)\d+ digits preceded by $
(?<!-)\b\d+ numbers without a minus sign
^(?=.*\d)(?=.*[A-Z]).{8,}$ 8+ characters with a digit and an uppercase letterFlags
Flags change how the whole pattern behaves. In JavaScript they follow the closing slash, as in /pattern/gi.
g global: find every match, not just the first
i ignore case
m multiline: ^ and $ match at each line
s dotAll: . also matches line breaks
u Unicode: code points, \p{…} and \u{…}
v Unicode sets: u plus [a--b] and [a&&b]
y sticky: match only at lastIndex
d indices: record each group's start and endReplacement references
In a replacement string, $1 and $<name> insert what a group captured. This swaps the order of names or reformats dates in one step. Test replacements in the regex tester's Replace tab, which highlights every change before you copy the result.
Pattern: (\d{4})-(\d{2})-(\d{2})
Replacement: $3/$2/$1
2026-11-27 → 27/11/2026
$& the whole match $$ a literal $ $` before the match $' after itReference: MDN: Regular expressions.