Regex Syntax Cheatsheet
Comprehensive cheatsheet for regular expression syntax and assertions
Comprehensive cheatsheet for regular expression syntax and assertions
Quick reference guide for regular expression syntax, character classes, quantifiers, and lookaround assertions.
.Character ClassesAny character except newline (unless 's' flag is set)
a.c matches abc, a1c
\dCharacter ClassesAny digit [0-9]
\d+ matches 123
\DCharacter ClassesAny non-digit [^0-9]
\D+ matches ABC
\wCharacter ClassesAny word character [a-zA-Z0-9_]
\w+ matches user_1
\WCharacter ClassesAny non-word character
\W matches ! or space
\sCharacter ClassesAny whitespace character (space, tab, newline)
\s+ matches spaces
\SCharacter ClassesAny non-whitespace character
\S+ matches text
[abc]Character ClassesAny character in the set (a, b, or c)
[aeiou] matches vowels
[^abc]Character ClassesAny character NOT in the set
[^0-9] matches non-digits
[a-z]Character ClassesCharacter range from a to z
[0-9a-fA-F] matches hex
*Quantifiers0 or more times (greedy)
a* matches '', a, aaa
+Quantifiers1 or more times (greedy)
a+ matches a, aaa
?Quantifiers0 or 1 time (optional)
colou?r matches color and colour
{n}QuantifiersExactly n times
\d{4} matches 2026
{n,}Quantifiersn or more times
\d{2,} matches 12, 123
{n,m}QuantifiersBetween n and m times
\d{2,4} matches 12, 123, 1234
*?Quantifiers0 or more times (lazy / non-greedy)
<.*?> matches <b> not <b>hello</b>
+?Quantifiers1 or more times (lazy / non-greedy)
a+? matches 'a' in 'aaaa'
^AnchorsStart of string (or line if 'm' flag set)
^Hello matches Hello at start
$AnchorsEnd of string (or line if 'm' flag set)
end$ matches end at line end
\bAnchorsWord boundary
\bcat\b matches cat not catch
\BAnchorsNon-word boundary
\Bcat\B matches certificate
(abc)GroupingCapturing group
(\d{2})-(\d{2}) captures parts
(?:abc)GroupingNon-capturing group
(?:https?|ftp):// matches protocol
(?<name>abc)GroupingNamed capturing group
(?<year>\d{4}) captures year
\1GroupingBackreference to group #1
(["']).*?\1 matches quoted strings
a|bGroupingAlternation (match either a or b)
cat|dog matches cat or dog
(?=abc)LookaroundPositive lookahead (followed by abc)
\d+(?=px) matches 100 in 100px
(?!abc)LookaroundNegative lookahead (not followed by abc)
\d+(?!px) matches 100 in 100em
(?<=abc)LookaroundPositive lookbehind (preceded by abc)
(?<=\$)\d+ matches 50 in $50
(?<!abc)LookaroundNegative lookbehind (not preceded by abc)
(?<!\$)\d+ matches 50 in €50
gFlagsGlobal: don't return after first match
/a/g matches all 'a's
iFlagsCase-insensitive matching
/cat/i matches cat, CAT, Cat
mFlagsMultiline: ^ and $ match line boundaries
Handles multi-line input
sFlagsDotAll: allows '.' to match newline characters
/<p>.*<\/p>/s
uFlagsUnicode: treats pattern as a sequence of unicode code points
/\p{Emoji}/u