All cheatsheets

Cheatsheets

Regular expressions

Regex tokens — character classes, anchors, quantifiers, groups, and lookarounds.

43 entries

Character classes7

.

Any character except newline

\d

A digit (0-9)

\D

A non-digit

\w

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

\W

A non-word character

\s

A whitespace character

\S

A non-whitespace character

Anchors4

^

Start of string / line

$

End of string / line

\b

A word boundary

\B

Not a word boundary

Quantifiers7

*

0 or more of the preceding token

+

1 or more of the preceding token

?

0 or 1 (optional)

*? +?

Lazy (match as few as possible)

{n}

Exactly n times

{n,}

n or more times

{n,m}

Between n and m times

Sets & groups8

[abc]

Any one of a, b, or c

[^abc]

Any character except a, b, c

[a-z]

Any character in the range a-z

(...)

Capturing group

(?:...)

Non-capturing group

(?<name>...)

Named capturing group

a|b

Match a or b

\1

Backreference to group 1

Lookaround & escape5

(?=...)

Positive lookahead

(?!...)

Negative lookahead

(?<=...)

Positive lookbehind

(?<!...)

Negative lookbehind

\

Escape the next metacharacter

Flags6

g

Global — find all matches

i

Case-insensitive

m

Multiline — ^ and $ match each line

s

Dotall — . also matches newline

u

Unicode mode

y

Sticky — match at lastIndex only

Common patterns6

^[\w.+-]+@[\w-]+\.[\w.-]+$

Email (rough)

https?:\/\/[^\s]+

URL

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

IPv4 address

^#?[0-9a-fA-F]{6}$

Hex color

\d{4}-\d{2}-\d{2}

ISO date (YYYY-MM-DD)

^[a-z0-9]+(?:-[a-z0-9]+)*$

Slug