Skip to content

Regex Cheatsheet

A complete Regular Expressions cheatsheet from beginner to advanced — character classes, quantifiers, groups, lookahead/lookbehind, flags, common patterns, and practical examples for JavaScript, Python, and other flavors.

Regular Expressions (regex) are powerful patterns for matching, searching, validating, and transforming text. This cheatsheet covers beginner fundamentals through advanced techniques used in JavaScript, Python, PCRE, and most modern flavors.

abc # matches the literal characters a, b, c
. # any character except newline
\. # literal dot
\\ # literal backslash

[abc] # a, b, or c
[^abc] # anything except a, b, or c
[a-z] # lowercase letter
[A-Z] # uppercase letter
[0-9] # digit
[a-zA-Z0-9] # alphanumeric

\d # digit [0-9]
\D # non-digit
\w # word character [a-zA-Z0-9_]
\W # non-word character
\s # whitespace (space, tab, newline…)
\S # non-whitespace
\n # newline
\t # tab
\r # carriage return

a? # 0 or 1 of a (optional)
a* # 0 or more of a (greedy)
a+ # 1 or more of a (greedy)
a{3} # exactly 3 of a
a{2,5} # 2 to 5 of a
a{2,} # 2 or more of a
# Lazy (non-greedy) versions
a?? a*? a+? a{2,5}?

Greedy vs Lazy example:

Text: <div>hello</div><div>world</div>
Greedy: <div>.*</div> → matches the whole string
Lazy: <div>.*?</div> → matches only the first <div>...</div>

^ # start of string (or line in multiline mode)
$ # end of string (or line in multiline mode)
\b # word boundary
\B # not a word boundary
\A # start of string (Python/PCRE, ignores multiline)
\Z # end of string (Python/PCRE)

Examples:

^\d+$ # entire string is one or more digits
\bcat\b # the whole word "cat"

(abc) # capturing group
(?:abc) # non-capturing group
(?<name>abc) # named capturing group (JS, Python, PCRE…)

\1 # reference to first capturing group
\k<name> # reference to named group (many flavors)
$1 $2 # replacement-side references (common)

Example – matching repeated words:

\b(\w+)\s+\1\b # e.g. "the the"

cat|dog|bird # match one of the alternatives
(?:cat|dog)s # non-capturing alternation

(?=...) # positive lookahead
(?!...) # negative lookahead
(?<=...) # positive lookbehind
(?<!...) # negative lookbehind

Examples:

\d+(?=px) # numbers followed by "px" (does not include px)
\d+(?!px) # numbers not followed by "px"
(?<=\$)\d+ # numbers preceded by $
(?<!@)\b\w+\b # words not preceded by @

Note: JavaScript supports fixed-length lookbehind (ES2018+). Variable-length lookbehind is available in Python, PCRE, and recent JS engines.

Flag Name Meaning
i Ignore case Case-insensitive matching
g Global Find all matches (not just the first)
m Multiline ^ and $ match start/end of each line
s DotAll . also matches newline
u Unicode Full Unicode support
y Sticky Match only from lastIndex (JavaScript)
x Extended Ignore whitespace & allow comments (PCRE…)

Examples:

/hello/i
/\d+/g
/^error/m

# Email (simple, practical)
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
# URL (basic)
https?:\/\/[^\s/$.?#].[^\s]*
# IP address (IPv4)
\b(?:\d{1,3}\.){3}\d{1,3}\b
# Date (YYYY-MM-DD)
\b\d{4}-\d{2}-\d{2}\b
# Time (HH:MM 24h)
\b([01]?\d|2[0-3]):[0-5]\d\b
# Hex color
#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})
# Username (alphanumeric + underscore, 3-16 chars)
^[a-zA-Z0-9_]{3,16}$
# Password (min 8 chars, at least one letter and one number)
^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$
# Trailing whitespace
[ \t]+$
# Empty lines
^\s*$

Common replacement syntax:

$0 or $& # whole match
$1, $2… # captured groups
$` # text before match
$' # text after match
$$ # literal $

Example (JavaScript):

"2026-11-11".replace(/(\d{4})-(\d{2})-(\d{2})/, "$3/$2/$1")
// → "11/11/2026"

\p{L} # any letter
\p{N} # any number
\p{Emoji} # emoji (where supported)
\P{L} # not a letter

(?>...) # atomic group (no backtracking)
a++ # possessive quantifier

(?(condition)yes|no)

Feature JavaScript Python PCRE/PHP
Named groups Yes (?<name>) Yes Yes
Lookbehind Fixed length* Variable Variable
\A \Z No Yes Yes
Atomic groups No No Yes
Possessive quantifiers No No Yes
Recursive patterns No No Yes
x (extended) flag No Yes (re.X) Yes

* Modern JS engines support variable-length lookbehind.

  1. Use online tools: regex101.com, regexr.com, debuggex.com
  2. Start simple and build up incrementally
  3. Prefer character classes over long alternations when possible
  4. Be careful with catastrophic backtracking ((a+)+b on long strings)
  5. Use non-capturing groups (?:...) when you don’t need the capture
  6. Anchor your patterns (^…$) when validating whole strings
  7. Document complex regex with comments (when the flavor supports x flag)

. any char except \n
\d \w \s digit / word / whitespace
[abc] character class
[^abc] negated class
* + ? 0+, 1+, 0-or-1 (greedy)
*? +? lazy versions
{n,m} quantifier range
^ $ start / end
\b word boundary
(...) capturing group
(?:...) non-capturing group
(?=...) positive lookahead
(?!...) negative lookahead
(?<=...) positive lookbehind
(?<!...) negative lookbehind
| alternation
\1 backreference


Regex is powerful but can become unreadable quickly. Prefer clear, well-commented patterns (or even multiple simpler passes) over a single dense expression when maintainability matters.

Frequently asked questions

What is the difference between greedy and lazy quantifiers?

Greedy quantifiers (* + ? {n,}) match as much as possible. Lazy (non-greedy) quantifiers (*? +? ?? {n,}?) match as little as possible.

What is the difference between a capturing group and a non-capturing group?

Capturing groups () save the matched text for later use. Non-capturing groups (?:) group patterns without saving the match.

What does the dot (.) match?

By default the dot matches any character except newline. Use the s (dotAll) flag or a character class that includes newline to also match newlines.

How do lookahead and lookbehind work?

Lookahead (?=...) and lookbehind (?<=...) assert that a pattern exists (or does not exist with !) without including it in the match.

Are regex flavors the same everywhere?

No. JavaScript, Python, PCRE, Java, and .NET have differences in lookbehind support, Unicode handling, named groups, and available flags.

You're offline Check your connection.