Regex Cheatsheet
Searchable reference of regex tokens, quantifiers, groups and lookarounds.
^ Start of string (or line with m flag).
^abc matches "abc" at start
$ End of string (or line with m flag).
abc$ matches "abc" at end
\b Word boundary.
\bcat\b matches "cat" not "category"
\B Non-word boundary.
\Bcat matches "cat" in "bobcat"
. Any character except newline.
a.c matches "abc", "axc"
[abc] Any one of a, b or c.
[aeiou] matches a vowel
[^abc] Any character except a, b or c.
[^0-9] matches a non-digit
[a-z] Character range.
[a-z] matches one lowercase letter
\d Any digit (0-9).
\d{4} matches "2026"
\D Any non-digit.
\D matches "a"
\w Word character [A-Za-z0-9_].
\w+ matches "user_1"
\W Non-word character.
\W matches "!"
\s Whitespace (space, tab, newline).
a\sb matches "a b"
\S Non-whitespace.
\S+ matches a word
* Zero or more (greedy).
ab* matches "a", "ab", "abb"
+ One or more (greedy).
a+ matches "a", "aaa"
? Zero or one (optional).
colou?r matches "color"/"colour"
{n} Exactly n times.
\d{3} matches "123"
{n,} n or more times.
\d{2,} matches "12", "1234"
{n,m} Between n and m times.
\d{2,4} matches "12" to "1234"
*? Lazy (non-greedy) zero or more.
<.*?> matches one tag at a time
+? Lazy one or more.
a+? matches as few "a" as possible
(abc) Capturing group.
(ab)+ captures "ab"
(?:abc) Non-capturing group.
(?:ab)+ groups without capturing
(?<name>abc) Named capturing group.
(?<year>\d{4})
a|b Alternation (a or b).
cat|dog matches either word
\1 Backreference to group 1.
(\w)\1 matches doubled letters
\k<name> Backreference to a named group.
(?<q>["']).*?\k<q>
(?=abc) Positive lookahead.
\d(?=px) matches digit before "px"
(?!abc) Negative lookahead.
\d(?!px) digit not before "px"
(?<=abc) Positive lookbehind.
(?<=\$)\d+ matches digits after "$"
(?<!abc) Negative lookbehind.
(?<!\$)\d+ digits not after "$"
\. Literal dot (escaped metachar).
example\.com
\/ Literal forward slash.
https?:\/\/
\n Newline character.
line1\nline2
\t Tab character.
col1\tcol2
\\ Literal backslash.
C:\\Windows
g Global — find all matches, not just the first.
/a/g
i Case-insensitive matching.
/abc/i matches "ABC"
m Multiline — ^ and $ match line boundaries.
/^a/m
s Dotall — . also matches newlines.
/a.b/s
u Unicode mode.
/\u{1F600}/u
y Sticky — match at lastIndex only.
/a/y