🏠 Home›Cheatsheets›Regex

🔍 Regex Cheatsheet

Regular expression patterns and syntax

Character Classes

[abc]

Match any single character: a, b, or c

/[abc]/ → matches "a" in "abc"

Negated Character Class

[^abc]

Match any character except a, b, or c

/[^abc]/ → matches "d" in "dbc"

Character Range

[a-z]

Match any lowercase letter

/[a-z]/ → matches "a" in "apple"

Digit

\d

Match any digit (0-9)

/\d/ → matches "5" in "abc123"

Non-Digit

\D

Match any non-digit character

/\D/ → matches "a" in "a1b2"

Whitespace

\s

Match whitespace (space, tab, newline)

/\s/ → matches space in "a b"

Non-Whitespace

\S

Match non-whitespace character

/\S/ → matches "a" in "a b"

Word Character

\w

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

/\w/ → matches "a" in "a-b"

Zero or More

*

Match 0 or more of previous element

/ab*c/ → matches "ac", "abc", "abbc"

One or More

+

Match 1 or more of previous element

/ab+c/ → matches "abc", "abbc" (not "ac")

Zero or One

?

Match 0 or 1 of previous element

/ab?c/ → matches "ac", "abc"

Exactly N Times

{n}

Match exactly n times

/a{3}/ → matches "aaa"

N to M Times

{n,m}

Match between n and m times

/a{2,4}/ → matches "aa", "aaa", "aaaa"

Alternation

|

Match either left or right

/cat|dog/ → matches "cat" or "dog"

Grouping

()

Group subpattern

/(ab)+/ → matches "ab", "abab"

Start of String

^

Match start of string

/^hello/ → matches "hello world"

End of String

$

Match end of string

/world$/ → matches "hello world"

Any Character

.

Match any character (except newline)

/a.c/ → matches "abc", "adc"

Email Pattern

[^@]+@[^@]+\.[^@]+

Simple email validation

/[^@]+@[^@]+\.[^@]+/ → matches "user@example.com"

URL Pattern

https?://[\w.-]+

Match HTTP/HTTPS URLs

/https?://[\w.-]+/ → matches "https://example.com"

More in this category

|🏠 Home|
|