Skip to main content

Regex Cheatsheet

Regex & Debugging

Interactive, searchable regex reference covering character classes, quantifiers, anchors, groups, lookaheads, and flags.

Character Classes

PatternDescriptionExample
.Any character except newlinea.c → abc, a1c
\dDigit [0-9]\d+ → 123, 42
\DNon-digit [^0-9]\D+ → abc, !@#
\wWord character [a-zA-Z0-9_]\w+ → hello_42
\WNon-word character\W+ → @#!, ...
\sWhitespace (space, tab, newline)a\sb → a b
\SNon-whitespace\S+ → hello
[abc]Character set — matches a, b, or c[aeiou] → vowels
[^abc]Negated set — anything except a, b, c[^0-9] → non-digits
[a-z]Range — lowercase letters[a-zA-Z] → letters

Quantifiers

PatternDescriptionExample
*0 or more (greedy)ab*c → ac, abc, abbc
+1 or more (greedy)ab+c → abc, abbc
?0 or 1 (optional)colou?r → color, colour
{n}Exactly n times\d{3} → 123
{n,}n or more times\d{2,} → 12, 123, 1234
{n,m}Between n and m times\d{2,4} → 12, 123, 1234
*?0 or more (lazy/non-greedy)<.*?> → first tag only
+?1 or more (lazy/non-greedy)".+?" → first quoted
??0 or 1 (lazy)colou??r → prefers color

Anchors

PatternDescriptionExample
^Start of string (or line with m flag)^Hello → starts with Hello
$End of string (or line with m flag)world$ → ends with world
\bWord boundary\bword\b → whole word
\BNon-word boundary\Bword → mid-word only

Groups & References

PatternDescriptionExample
(abc)Capturing group(ha)+ → haha
(?:abc)Non-capturing group(?:ha)+ → haha
(?<name>abc)Named capturing group(?<year>\d{4})
\1Backreference to group 1(\w+)\s\1 → word word
\k<name>Named backreference\k<year>
a|bAlternation (or)cat|dog → cat or dog

Lookaheads & Lookbehinds

PatternDescriptionExample
(?=abc)Positive lookahead\d(?=px) → 3 in 3px
(?!abc)Negative lookahead\d(?!px) → 3 not before px
(?<=abc)Positive lookbehind(?<=\$)\d+ → 99 in $99
(?<!abc)Negative lookbehind(?<!\$)\d+ → not after $

Flags

PatternDescriptionExample
gGlobal — find all matches, not just first/a/g → all a's
iCase-insensitive matching/hello/i → Hello, HELLO
mMultiline — ^ and $ match line boundaries/^line/m → each line
sDotAll — . also matches newline/a.b/s → a\nb
uUnicode — full Unicode matching/\u{1F600}/u → 😀
ySticky — match at exact positionlastIndex anchored