Regex Cheatsheet

Regular expression syntax reference with common pattern examples.

26 patterns

Literal match

Basics

Match exact text. Case-sensitive by default.

hello

Example: Say hello world

Case insensitive

Basics

The i flag makes the match case-insensitive.

/hello/i

Example: Hello HELLO hello

Any character

Basics

. matches any single character except newline.

h.llo

Example: hello hallo hxllo

Start of string

Anchors

^ anchors the match to the start of the string.

^hello

Example: hello world (not: say hello)

End of string

Anchors

$ anchors the match to the end of the string.

world$

Example: hello world (not: world class)

Word boundary

Anchors

\b matches at a word boundary.

\bcat\b

Example: cat (not: category, scatter)

0 or more

Quantifiers

* matches 0 or more of the preceding element.

ab*c

Example: ac, abc, abbc, abbbc

1 or more

Quantifiers

+ matches 1 or more of the preceding element.

ab+c

Example: abc, abbc (not: ac)

0 or 1

Quantifiers

? makes the preceding element optional.

colou?r

Example: color, colour

Exactly n times

Quantifiers

{n} matches exactly n repetitions.

\d{4}

Example: 2024, 1999

Between n and m

Quantifiers

{n,m} matches between n and m repetitions.

\d{2,4}

Example: 12, 123, 1234

Digit

Character Classes

\d matches any digit. \D matches non-digit.

\d

Example: 0-9

Word char

Character Classes

\w matches word characters. \W matches non-word.

\w

Example: a-z, A-Z, 0-9, _

Whitespace

Character Classes

\s matches whitespace. \S matches non-whitespace.

\s

Example: space, tab, newline

Custom class

Character Classes

[] defines a character set. Match any one character listed.

[aeiou]

Example: a, e, i, o, u

Negated class

Character Classes

^ inside [] negates the set.

[^0-9]

Example: any non-digit

Capturing group

Groups

() captures matched text for backreferences or extraction.

(\d{4})-(\d{2})-(\d{2})

Example: 2024-03-15 → groups: 2024, 03, 15

Non-capturing group

Groups

(?:) groups without capturing.

(?:https?://)

Example: http:// or https://

Named group

Groups

(?<name>) creates a named capture group.

(?<year>\d{4})-(?<month>\d{2})

Example: groups.year, groups.month

Alternation (OR)

Groups

| acts as OR between alternatives.

cat|dog|bird

Example: cat, dog, bird

Lookahead

Lookarounds

(?=...) asserts what follows without consuming it.

\d+(?= dollars)

Example: 100 in '100 dollars'

Negative lookahead

Lookarounds

(?!...) asserts what does NOT follow.

\d+(?! dollars)

Example: 100 in '100 euros'

Email pattern

Common Patterns

Basic email validation pattern.

[\w.-]+@[\w.-]+\.[a-z]{2,}

Example: user@example.com

URL pattern

Common Patterns

Simple URL matching pattern.

https?://[\w.-]+(?:\.[a-z]{2,})+(?:/[\S]*)?

Example: https://example.com/path

IPv4 address

Common Patterns

Match an IPv4 address (does not validate range 0-255).

(?:\d{1,3}\.){3}\d{1,3}

Example: 192.168.1.1

Hex color

Common Patterns

Match hex color codes.

#[0-9A-Fa-f]{3,6}

Example: #fff, #3a7bcd

All processing happens locally in your browser. No data is sent to any server.