Regex Cheat Sheet

A comprehensive guide to regular expressions with patterns, examples, and explanations

Anchors

^

Start of input or line (with multiline flag)

\A

Start of string only

$

End of input or line (with multiline flag)

\Z

End of string only

\b

Word boundary (between word and non-word)

\B

Non-word boundary

\<

Start of word

\>

End of word

Character Classes

\c

Control character (e.g., \cM for Control-M)

\s

Whitespace (space, tab, line feed)

\S

Non-whitespace character

\d

Digit (0-9)

\D

Non-digit character

\w

Word character (A-Z, a-z, 0-9, _)

\W

Non-word character

\x

Hex character (e.g., \x41 for "A")

\O

Octal character (e.g., \101 for "A")

\p{L}

Any letter (Unicode)

\p{N}

Any number (Unicode)

\p{P}

Any punctuation (Unicode)

\p{S}

Any symbol (Unicode)

Quantifiers

*

0 or more (greedy)

+

1 or more (greedy)

?

0 or 1 (greedy)

{3}

Exactly 3 times

{3,}

3 or more times

{3,5}

3 to 5 times

*?

0 or more (lazy)

+?

1 or more (lazy)

??

0 or 1 (lazy)

Groups and Ranges

.

Any character except line terminators

(a|b)

Either "a" or "b"

(...)

Capturing group

(?:...)

Non-capturing group

(?<name>...)

Named capturing group

\k<name>

Reference to named group

[abc]

Any of a, b, or c

[^abc]

Not a, b, or c

[a-q]

Any lowercase letter a to q

[A-Q]

Any uppercase letter A to Q

[0-7]

Any digit 0 to 7

Assertions

x(?=y)

x followed by y (lookahead)

x(?!y)

x not followed by y (negative lookahead)

(?<=y)x

x preceded by y (lookbehind)

(?<!y)x

x not preceded by y (negative lookbehind)

Special Characters

\n

Line feed (U+000A)

\r

Carriage return (U+000D)

\t

Tab (U+0009)

\v

Vertical tab (U+000B)

\f

Form feed (U+000C)

\xxx

Octal character (e.g., \101 for "A")

\xhh

Hex character (e.g., \x41 for "A")

\u{hhhh}

Unicode character (e.g., \u{1F600} for "😀")

Flags

g

Global search (all matches)

i

Case-insensitive

m

Multiline (^ and $ match line starts/ends)

s

Dot matches newlines

u

Unicode support

y

Sticky search (from lastIndex)

d

Generate match indices