Skip to content

Regex cheat sheet

Regular expression syntax as used by JavaScript, Python (re), PCRE and most editors. Try any pattern in the live regex tester.

27 entries

Characters

.

Any character except newline (with the s flag: including newline).

\d  \w  \s

Digit, word character [A-Za-z0-9_], whitespace.

\D  \W  \S

Negations of the above.

[abc]  [a-z]  [^0-9]

Character set, range, negated set.

\.  \*  \\

Escape special characters with a backslash.

Anchors

^  $

Start / end of string (of each line with the m flag).

\b  \B

Word boundary / not a word boundary.

Quantifiers

*  +  ?

0 or more, 1 or more, 0 or 1.

{3}  {2,}  {2,5}

Exactly 3, 2 or more, between 2 and 5.

*?  +?  {2,5}?

Lazy versions: match as little as possible.

Groups & alternation

(abc)

Capturing group, available as $1 / \1.

(?:abc)

Non-capturing group.

(?<year>\d{4})

Named group (JS, Python uses (?P<year>…)).

cat|dog

Alternation: either side.

(\w)\1

Backreference: repeated character, e.g. "ll".

Lookarounds

\d+(?=px)

Lookahead: digits followed by px.

\d+(?!px)

Negative lookahead.

(?<=\$)\d+

Lookbehind: digits preceded by $.

(?<!-)\b\d+

Negative lookbehind.

Flags

g  i  m  s  u  y

Global, case-insensitive, multiline, dotAll, unicode, sticky (JavaScript).

re.I  re.M  re.S  re.X

Python equivalents; re.X allows comments and whitespace.

Common patterns

^[^\s@]+@[^\s@]+\.[^\s@]+$

Pragmatic email check (real validation = send an email).

https?:\/\/[^\s/$.?#].[^\s]*

URL in text.

^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$

ISO date YYYY-MM-DD (does not check month lengths).

^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$

Hex colour.

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$

Password: 8+ chars with lower, upper and digit.

^\s+|\s+$

Leading or trailing whitespace (use with g to trim).

Frequently asked questions

What is the difference between greedy and lazy quantifiers?

Greedy quantifiers (*, +) match as much text as possible and then back off; lazy ones (*?, +?) match as little as possible. For "<a><b>", <.*> matches the whole string while <.*?> matches only "<a>".

Are regex patterns the same in every language?

The core syntax is shared, but details differ: Python writes named groups as (?P<name>…), older JavaScript engines lacked lookbehind, and POSIX tools like grep use a smaller syntax unless you pass -E or -P.

Where can I test a regex?

Use the QuickRef.dev regex tester: it highlights matches live, lists capture groups and explains each token of your pattern.

Related cheat sheets