All References

Regex Cheatsheet

Quick reference for regular expression syntax — anchors, character classes, quantifiers, groups, lookarounds, and flags. Applies to JavaScript, Python, and PCRE.

Anchors

Pattern Description Example
^ Start of string (or line with m flag) ^Hello matches "Hello world"
$ End of string (or line with m flag) end$ matches "the end"
\b Word boundary (between \w and \W) \bcat\b matches "cat" not "catch"
\B Non-word boundary \Bcat\B matches "cat" inside "concatenate"
\A Start of string (Python/PCRE, never multiline) \AHello
\Z End of string (Python/PCRE) end\Z

Character Classes

Pattern Matches Equivalent
. Any character except newline (use s flag to include newline)
\d Digit [0-9]
\D Non-digit [^0-9]
\w Word character [a-zA-Z0-9_]
\W Non-word character [^a-zA-Z0-9_]
\s Whitespace [ \t\r\n\f\v]
\S Non-whitespace [^ \t\r\n\f\v]
[abc] Any of a, b, or c Character set
[^abc] Not a, b, or c Negated character set
[a-z] Any lowercase letter Range inside character set
[a-zA-Z0-9] Alphanumeric character Combined ranges

Quantifiers

Quantifiers are greedy by default — they match as much as possible. Add ? to make them lazy (match as little as possible).

Greedy Lazy Meaning
* *? 0 or more
+ +? 1 or more
? ?? 0 or 1 (optional)
{n} Exactly n times
{n,} {n,}? n or more times
{n,m} {n,m}? Between n and m times

Groups & References

Syntax Description Example
(...) Capturing group (foo|bar)
(?:...) Non-capturing group (?:foo|bar)
(?<name>...) Named capturing group (JS/PCRE) (?<year>\d{4})
(?P<name>...) Named capturing group (Python) (?P<year>\d{4})
\1, \2 Backreference to group 1, 2… (\w+) \1 matches "hello hello"
$1, $2 Group reference in replacement string JS: "$1-$2"
a|b Alternation — match a or b cat|dog

Lookarounds

Lookarounds are zero-width assertions — they check what's around a position without consuming characters.

Syntax Type Matches when… Example
(?=...) Positive lookahead … is ahead \d+(?= dollars)
(?!...) Negative lookahead … is NOT ahead \d+(?! dollars)
(?<=...) Positive lookbehind … is behind (?<=\$)\d+
(?<!...) Negative lookbehind … is NOT behind (?<!\$)\d+

Flags

Flag Name Effect
g Global Find all matches, not just the first
i Case-insensitive Hello matches "hello", "HELLO", etc.
m Multiline ^ and $ match start/end of each line
s Dotall . also matches newline characters
u Unicode Treat pattern as Unicode code points (JS)
y Sticky Match only at lastIndex position (JS)
x Extended / verbose Allow whitespace and comments in pattern (Python/PCRE)

Common Patterns

Use Case Pattern
Email address [a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}
URL https?:\/\/[^\s/$.?#].[^\s]*
IPv4 address \b(?:\d{1,3}\.){3}\d{1,3}\b
Date (YYYY-MM-DD) \d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])
Hex color #(?:[0-9a-fA-F]{3}){1,2}
Slug ^[a-z0-9]+(?:-[a-z0-9]+)*$
UUID v4 [0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}

Test your expressions live with the Regex Tester tool.

Frequently Asked Questions

What is a regex anchor?

A regex anchor is a zero-width assertion that matches a position in the string rather than a character. The most common anchors are ^ (start of string or line) and $ (end of string or line). Word boundaries (\b) are also anchors — they match the boundary between a word character and a non-word character. Anchors do not consume any characters; they only assert that the engine is at a specific position.

What is the difference between * and + quantifiers?

The * quantifier matches zero or more occurrences of the preceding element, meaning the element is optional. The + quantifier matches one or more occurrences, meaning at least one match is required. For example, \d* matches an empty string, but \d+ requires at least one digit. Both are greedy by default; add ? (*? or +?) for lazy matching.

What are non-capturing groups?

Non-capturing groups, written as (?:...), group part of a pattern for the purposes of applying quantifiers or alternation without creating a backreference. They are more efficient than capturing groups because the engine does not store the matched text. Use them whenever you need grouping but do not need to reference the matched substring later. For example, (?:foo|bar)+ matches one or more occurrences of "foo" or "bar" without capturing.