Regex Cheat Sheet

Complete reference guide for regular expression syntax with examples

Anchors

SyntaxDescriptionExampleTry
^Start of string or line^Hello matches 'Hello world'Try it →
$End of string or lineworld$ matches 'Hello world'Try it →
\bWord boundary\bword\b matches whole word 'word'Try it →
\BNon-word boundary\Bword matches 'sword' but not 'word'Try it →

Character Classes

SyntaxDescriptionExampleTry
[abc]Match any character in brackets[aeiou] matches vowelsTry it →
[^abc]Match any character NOT in brackets[^0-9] matches non-digitsTry it →
[a-z]Match any character in range[a-zA-Z] matches lettersTry it →
.Any character except newlinea.c matches 'abc', 'a1c', etc.Try it →
\wWord character [a-zA-Z0-9_]\w+ matches 'hello_123'Try it →
\WNon-word character\W matches '@', ' ', etc.Try it →
\dDigit [0-9]\d{3} matches '123'Try it →
\DNon-digit\D+ matches 'abc'Try it →
\sWhitespace (space, tab, newline)\s+ matches spacesTry it →
\SNon-whitespace\S+ matches 'word'Try it →

Quantifiers

SyntaxDescriptionExampleTry
*0 or more (greedy)a* matches '', 'a', 'aaa'Try it →
+1 or more (greedy)a+ matches 'a', 'aaa' but not ''Try it →
?0 or 1 (optional)colou?r matches 'color' and 'colour'Try it →
{n}Exactly n times\d{4} matches '2024'Try it →
{n,}n or more times\d{2,} matches '12', '123', '1234'Try it →
{n,m}Between n and m times\d{2,4} matches '12', '123', '1234'Try it →
*?0 or more (lazy)a*? matches minimal 'a'sTry it →
+?1 or more (lazy)<.+?> matches single HTML tagTry it →
??0 or 1 (lazy)Matches minimal optionalTry it →

Groups & Capturing

SyntaxDescriptionExampleTry
(abc)Capturing group(\d+) captures digitsTry it →
(?:abc)Non-capturing group(?:Mr|Ms)\.? matches titleTry it →
(?<name>abc)Named capturing group(?<year>\d{4}) captures as 'year'Try it →
\1Backreference to group 1(\w)\1 matches 'aa', 'bb'Try it →
\k<name>Backreference to named group(?<char>\w)\k<char>Try it →
(a|b)Alternation (OR)(cat|dog) matches 'cat' or 'dog'Try it →

Lookahead & Lookbehind

SyntaxDescriptionExampleTry
(?=abc)Positive lookahead\d(?=px) matches digit before 'px'Try it →
(?!abc)Negative lookahead\d(?!px) matches digit NOT before 'px'Try it →
(?<=abc)Positive lookbehind(?<=\$)\d+ matches digits after '$'Try it →
(?<!abc)Negative lookbehind(?<!\$)\d+ matches digits NOT after '$'Try it →

Flags / Modifiers

SyntaxDescriptionExampleTry
gGlobal - find all matches/a/g finds all 'a' in 'banana'Try it →
iCase-insensitive/hello/i matches 'HELLO'Try it →
mMultiline - ^ and $ match line boundaries/^start/mTry it →
sDotall - . matches newlines/a.b/s matches 'a\nb'Try it →
uUnicode support/\p{L}/u matches Unicode lettersTry it →
ySticky - match at lastIndex onlyFor iterative matchingTry it →

Special Characters & Escapes

SyntaxDescriptionExampleTry
\Escape special character\. matches literal dotTry it →
\nNewlineline1\nline2Try it →
\rCarriage returnWindows line ending \r\nTry it →
\tTabcolumn1\tcolumn2Try it →
\0Null characterString terminatorTry it →
\xhhHex character\x41 matches 'A'Try it →
\uhhhhUnicode character\u0041 matches 'A'Try it →

Substitution / Replacement

SyntaxDescriptionExampleTry
$1, $2Insert captured group'(\w+) (\w+)' → '$2 $1'Try it →
$&Insert entire match'\d+' → '[$&]' wraps numbersTry it →
$`Insert text before matchBefore the matchTry it →
$'Insert text after matchAfter the matchTry it →
$$Insert literal $'price' → '$$99'Try it →
$<name>Insert named group'(?<first>\w+)' → '$<first>'Try it →