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 a line break (JavaScript excludes \n, \r, U+2028 and U+2029; Python excludes only \n). With the s flag it matches line breaks too
\d / \DA digit / a non-digit
\w / \WWord char / non-word char. In JavaScript \w is [A-Za-z0-9_], so it skips accented and non-Latin letters (use a \p{…} property escape with the u flag for those); Python str patterns also match Unicode letters and digits
\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). Python and PCRE also match just before a newline at the very end of the string; JavaScript does not
\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 (Python spells it (?P<name>abc))
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 JavaScript flags: g (global, find all), i (case-insensitive), m (multiline, ^/$ match each line), and s (dot also matches newlines). Python has no g flag: use re.findall or re.finditer to get every match, and re.I, re.M and re.S (or inline (?i), (?m), (?s)) for the others.

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.

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, which is what you want when the shortest match is the right one.