Regex Cheat Sheet
Every metacharacter, quantifier, class and assertion with a worked example for each.
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] Quick reference
| Pattern | Matches | Example |
|---|---|---|
. | Any character except newline | a.c matches abc, a1c |
\d \w \s | Digit, word char, whitespace | \d{3} matches 123 |
\D \W \S | The negation of each | \S+ matches a run of non-space |
[abc] | Any one of these | [aeiou] matches a vowel |
[^abc] | Any one character except these | [^0-9] matches a non-digit |
[a-z] | A range | [a-fA-F0-9] matches a hex digit |
* | Zero or more | ab*c matches ac, abc, abbc |
+ | One or more | ab+c matches abc but not ac |
? | Zero or one | colou?r matches both spellings |
{n,m} | Between n and m times | \d{3,5} matches 3 to 5 digits |
*? +? ?? | Lazy — as few as possible | <.+?> stops at the first > |
^ $ | Start / end of string | ^abc$ matches only "abc" |
\b | Word boundary | \bcat\b does not match "concatenate" |
(…) | Capturing group | (\d{4})-(\d{2}) captures both parts |
(?:…) | Group without capturing | Use when you only need the grouping |
(?<name>…) | Named group | (?<year>\d{4}) |
| | Alternation | cat|dog |
(?=…) | Positive lookahead | \d+(?= USD) matches digits before " USD" |
(?!…) | Negative lookahead | foo(?!bar) |
(?<=…) | Positive lookbehind | (?<=£)\d+ matches digits after £ |
(?<!…) | Negative lookbehind | (?<!un)happy |
\1 | Backreference | (\w)\1 matches a doubled letter |
Principles that prevent most regex bugs
- Anchor unless you mean not to —An unanchored pattern matches anywhere. Validating a postcode without ^ and $ will happily accept a valid postcode buried inside a paragraph of nonsense.
- Prefer a negated class to a lazy dot —
"[^"]*"is faster and clearer than".*?", and it cannot run past the closing delimiter. - Avoid nested quantifiers over overlapping alternatives —
(a+)+and(\w+\s?)*can take exponential time on non-matching input. This is a denial-of-service bug, not a performance nit. - Name your groups —Positional groups break silently the moment someone inserts a group earlier in the pattern. Named ones do not.
- Use the x / verbose flag for anything long —Most languages support a mode where whitespace and comments inside the pattern are ignored, which turns an unreadable one-liner into something maintainable. JavaScript does not have it, which is why complex JS patterns should be composed from named sub-strings.
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.
More in Reference & Data
HTTP Status Codes Every status code with its meaning, the RFC that defines it, and notes on when to use each rather than a near neighbour.MIME Types Media types mapped to file extensions in both directions, covering the full IANA registry.HTML Entities Named and numeric character references with a live preview of each rendered glyph.Port Numbers Well-known and registered TCP/UDP ports with the service on each and its transport protocol.Unicode Explorer Search by code point, name or character, with the UTF-8, UTF-16 and HTML encodings of each shown.ASCII Table All 128 ASCII characters in decimal, hex, octal and binary, including the control characters.
See all reference & data.