RUNWEBTOOLS
English

Regex Cheat Sheet: Regular Expression Syntax Reference

Regular expressions (regex) are patterns for matching text — used for search-and-replace, validation, and extraction. The syntax below is the common flavor shared by JavaScript, Python, PCRE, and most modern engines. Search any token to jump to it.

Updated 2026-07-06

Character classes

TokenMatchesExample
.Any character except a newlinea.c → abc, a7c
\dAny digit (0–9)\d\d → 42
\DAny non-digit\D → a, %
\wWord character (letter, digit, underscore)\w+ → hello_1
\WNon-word character\W → space, !
\sWhitespace (space, tab, newline)a\sb → a b
\SNon-whitespace\S+ → word
[abc]Any one of a, b, or c[aeiou] → a vowel
[^abc]Any character except a, b, c[^0-9] → non-digit
[a-z]Any character in the range[a-f] → a…f

Anchors & boundaries

TokenMatchesExample
^Start of the string (or line, in multiline mode)^Hi → line starting Hi
$End of the string (or line)end$ → line ending end
\bWord boundary\bcat\b → the word cat
\BNot a word boundary\Bcat → scatter

Quantifiers

TokenMatchesExample
*0 or more of the precedinga* → '', a, aaa
+1 or morea+ → a, aaa (not '')
?0 or 1 (optional)colou?r → color, colour
{n}Exactly n times\d{4} → 2026
{n,}n or more times\d{2,} → 42, 4200
{n,m}Between n and m times\d{2,4} → 42 to 4200
*?Lazy: as few as possible<.+?> → shortest match

Groups, alternation & lookarounds

TokenMatchesExample
(abc)Capturing group(ab)+ → abab
(?:abc)Non-capturing group(?:ab)+ → group, no capture
(?<name>…)Named capturing group(?<year>\d{4})
a|bAlternation — a or bcat|dog → cat, dog
\1Backreference to group 1(\w)\1 → ll, oo
(?=abc)Lookahead — followed by abc\d(?=px) → 5 in 5px
(?!abc)Negative lookahead — not followed by\d(?!px)
(?<=abc)Lookbehind — preceded by abc(?<=\$)\d+ → 9 in $9
(?<!abc)Negative lookbehind(?<!\$)\d+

Flags

TokenMatchesExample
gGlobal — find all matches, not just the first/a/g
iCase-insensitive/abc/i → ABC
mMultiline — ^ and $ match line breaks/^x/m
sDotall — . also matches newlines/a.b/s
uUnicode mode/\u{1F600}/u

A note on escaping

To match a character that has special meaning — such as . * + ? ( ) [ ] { } ^ $ | \\ — put a backslash in front of it. For example, \\. matches a literal dot and \\$ matches a literal dollar sign. When a pattern isn't matching what you expect, an unescaped special character is the usual cause.

Related tools