Tools / Regex & Patterns / Regex Cheat Sheet

Regex Cheat Sheet

Tokens

.

Any character except line break (unless the s flag is set).

a.c matches abc, axc

\d

A digit, 0-9.

\d+ matches 123

\D

Any non-digit character.

\D+ matches abc

\w

A word character: letters, digits, or underscore.

\w+ matches hello_1

\W

Any non-word character.

\W matches @, (space)

\s

A whitespace character (space, tab, newline).

\s+ matches the gaps in a b

\S

Any non-whitespace character.

\S+ matches hello

^

Start of the string (or of a line, with the m flag).

^Hello matches Hello world

$

End of the string (or of a line, with the m flag).

end$ matches the end

\b

A word boundary — between a word char and a non-word char.

\bcat\b matches cat but not category

\B

A non-word-boundary.

\Bcat\B matches cat inside concatenate

*

Zero or more of the preceding token.

ab*c matches ac, abc, abbbc

+

One or more of the preceding token.

ab+c matches abc, abbc, not ac

?

Zero or one of the preceding token.

colou?r matches color, colour

{n,m}

Between n and m repetitions of the preceding token.

a{2,4} matches aa, aaa, aaaa

[...]

A character class — any one of the characters listed.

[aeiou] matches any vowel

[^...]

A negated character class — any character not listed.

[^0-9] matches any non-digit

(...)

A capturing group.

(ab)+ matches ab, abab, captures ab

(?:...)

A non-capturing group — groups without saving the match.

(?:ab)+ matches ab, abab

(?<name>...)

A named capturing group.

(?<year>\d{4}) captures 2026 as year

|

Alternation — matches whatever is on either side.

cat|dog matches cat or dog

(?=...)

Lookahead — matches if followed by the pattern, without consuming it.

\d+(?=px) matches 42 in 42px

(?!...)

Negative lookahead — matches if not followed by the pattern.

\d+(?!px) does not match 42 in 42px

(?<=...)

Lookbehind — matches if preceded by the pattern, without consuming it.

(?<=\$)\d+ matches 42 in $42

(?<!...)

Negative lookbehind — matches if not preceded by the pattern.

(?<!\$)\d+ does not match 42 in $42

Flags

g

Global — find all matches instead of stopping at the first.

/a/g

i

Case-insensitive matching.

/abc/i matches ABC

m

Multiline — ^ and $ match at line breaks too, not just string start/end.

/^a/m

s

Dot-all — . also matches line break characters.

/a.b/s matches "a\nb"

u

Unicode — treats the pattern as a sequence of Unicode code points.

/\u{1F600}/u

y

Sticky — matches only starting at lastIndex, no scanning ahead.

/a/y

d

Generates start/end match indices for each capture group.

/a/d