Anchors
^ Start of string (or line with multiline flag) $ End of string (or line with multiline flag) \b Word boundary — between \w and \W \B Not a word boundary \A Start of string (no multiline, most engines) \Z End of string (no multiline, most engines) (?=...) Positive lookahead — followed by pattern (?!...) Negative lookahead — NOT followed by pattern (?<=...) Positive lookbehind — preceded by pattern (?<!...) Negative lookbehind — NOT preceded by pattern Character Classes
. Any character except newline (with /s also newline) \d Digit — [0-9] \D Not a digit — [^0-9] \w Word character — [a-zA-Z0-9_] \W Not a word character — [^a-zA-Z0-9_] \s Whitespace — space, tab, newline, etc. \S Not whitespace [abc] Character set — matches a, b, or c [^abc] Negated set — anything except a, b, or c [a-z] Range — any lowercase letter [a-zA-Z] Range — any letter (upper or lower) \p{L} Unicode letter (requires /u flag in JS) \p{N} Unicode number (requires /u flag in JS) Quantifiers
* 0 or more (greedy) + 1 or more (greedy) ? 0 or 1 (greedy) {n} Exactly n times {n,} n or more times {n,m} Between n and m times *? 0 or more (lazy / non-greedy) +? 1 or more (lazy) ?? 0 or 1 (lazy) {n,m}? Between n and m (lazy) *+ 0 or more (possessive, no backtrack) ++ 1 or more (possessive) Groups & Alternation
(abc) Capturing group — captures abc (?:abc) Non-capturing group — groups but does not capture (?<name>abc) Named capturing group \1 Backreference to group 1 \k<name> Named backreference a|b Alternation — matches a or b (?(1)yes|no) Conditional — if group 1 matched, use "yes" else "no" (?|a(b)|(c)d) Branch reset group (PCRE) Escape Sequences
\n Newline (LF) \r Carriage return (CR) \t Tab \v Vertical tab \f Form feed \0 Null byte \xhh Hex character (e.g. \x41 = A) \uhhhh Unicode character (e.g. \u0041 = A) \\ Literal backslash \. Literal dot (escape special chars) Flags / Modifiers
g Global — find all matches, not just the first i Case-insensitive matching m Multiline — ^ and $ match start/end of each line s Dotall — dot matches newline characters too u Unicode — enables full Unicode matching (\p{...}) y Sticky — match only at lastIndex position (JS) d Indices — populate match.indices (JS ES2022) x Extended — allow whitespace and comments (PCRE) Substitution (Replace)
$0 / $& Whole match $1, $2… Captured group 1, 2, etc. $<name> Named capture group in replacement $' String after the match $` String before the match $$ Literal dollar sign in replacement POSIX Classes (in [...])
[:alpha:] Letters — [a-zA-Z] [:digit:] Digits — [0-9] [:alnum:] Letters and digits [:space:] Whitespace characters [:upper:] Uppercase letters [:lower:] Lowercase letters [:punct:] Punctuation characters [:xdigit:] Hexadecimal digits [0-9a-fA-F] About
The Regex Cheatsheet is a quick-reference card for regular expression syntax, covering all the essential constructs: anchors (^ $), character classes (\d \w \s), quantifiers (*, +, ?, {n,m}), capturing and non-capturing groups, lookaheads, lookbehinds, backreferences, flags (g, i, m, s), and substitution patterns.
How to use
- 1 Browse the cards by category: Anchors, Character Classes, Quantifiers, Groups, Flags, etc.
- 2 Click any syntax example to copy it to your clipboard.
- 3 Use the search bar to quickly find a specific pattern or term.
- What is the difference between a greedy and a lazy quantifier?
- Greedy quantifiers (*, +, ?) match as much as possible. Lazy quantifiers (*?, +?, ??) match as little as possible. For example, given the string "<b>bold</b>", the greedy pattern <.*> matches the entire string, while the lazy pattern <.*?> matches just <b>. Add a ? after any quantifier to make it lazy.
- What is the difference between a capturing group and a non-capturing group?
- A capturing group (parentheses without ?) captures the matched text so it can be referenced by index ($1, $2, ...) in replacements or by group number in the match result. A non-capturing group (?:...) groups the pattern for quantifiers and alternation but does not capture the text, which is slightly faster and avoids polluting the match results.
- What is a lookahead and when would I use it?
- A positive lookahead (?=...) asserts that the text following the current position matches the pattern, without including it in the match. For example, \w+(?=ing) matches the word before "ing" without matching "ing" itself. A negative lookahead (?!...) asserts the opposite. Lookaheads are useful for validating password rules, extracting context-dependent patterns, and conditional replacements.