Regex Cheat Sheet

A regular-expression reference, character classes, anchors, quantifiers, groups and alternation, lookarounds, common ready-made patterns, and flags.

A reference for regular-expression syntax common to most engines (JavaScript, Python, PCRE). To test a pattern interactively, try our regex tester. To match a literal metacharacter, escape it with a backslash, e.g. \. for a dot.

Character classes

TokenMatches
.Any character except newline
\d / \DA digit / a non-digit
\w / \WWord char (a–z, 0–9, _) / non-word char
\s / \SWhitespace / non-whitespace
[abc]Any one of a, b, or c
[^abc]Any character except a, b, or c
[a-z0-9]A range: any lowercase letter or digit

Anchors & boundaries

TokenMatches
^Start of string (or line in multiline mode)
$End of string (or line in multiline mode)
\b / \BWord boundary / non-boundary

Quantifiers

TokenMatches
*0 or more (greedy)
+1 or more
?0 or 1 (optional)
{3}Exactly 3
{2,5}Between 2 and 5
{2,}2 or more
*? +?Lazy: match as few as possible

Groups & alternation

TokenMeaning
(abc)Capturing group (referenced as \1)
(?:abc)Non-capturing group
(?<name>abc)Named capturing group
a|bMatch a or b
(?=abc)Lookahead: followed by abc
(?!abc)Negative lookahead: not followed by abc
(?<=abc)Lookbehind: preceded by abc

Common patterns

PatternMatches
^\d{5}(-\d{4})?$US ZIP code (5 or 9 digit)
[\w.+-]+@[\w-]+\.[\w.-]+Email address (rough)
https?://[^\s]+An http or https URL
^\d{4}-\d{2}-\d{2}$Date in YYYY-MM-DD form
\b\d{1,3}(,\d{3})*\bNumber with thousands separators

Common flags: g (global, find all), i (case-insensitive), m (multiline, ^/$ match each line), and s (dot also matches newlines). Greedy quantifiers can backtrack a lot; prefer lazy versions or specific character classes for speed.

What this does

A regex cheat sheet lists the building blocks of regular expressions — character classes, anchors, quantifiers, groups, alternation, lookarounds, and flags.

How to use it

  1. Browse by token type.
  2. Find the pattern you need.
  3. Copy it with one tap.
  4. Test it against your text.

Example

^\d{3}-\d{4}$ matches a 7-digit phone like 555-1234.

Sources & methodology

Last updated .

Frequently asked questions

Does this work for JavaScript and Python?

Yes, the syntax shown is common to most modern engines, including JavaScript, Python, PCRE, and Java. A few advanced features differ between flavors.

How do I match a literal special character?

Escape it with a backslash. For example, \. matches a literal dot and \( matches a literal open parenthesis.

What is the difference between greedy and lazy?

Greedy quantifiers (like .*) match as much as possible; adding a ? makes them lazy (.*?), matching as little as possible. Lazy versions are often faster and more precise.