Regex Cheat Sheet
Every token, quantifier, class, group and lookaround on one searchable page — each with a live example you can run, and the flavour differences called out.
Search
43 tokensCharacter classes
What a single position is allowed to be..Any character except a newline.\dA digit, 0-9.\DAnything that is not a digit.\wA word character: letter, digit or underscore.\WAnything that is not a word character.\sWhitespace: space, tab, newline, and friends.\SAnything that is not whitespace.[abc]Any one of the listed characters.[^abc]Any character except the listed ones.[a-z]A range. Combine freely: [a-zA-Z0-9].\p{L}A Unicode property — here, any letter in any script.JavaScript: Requires the u or v flag.
Quantifiers
How many times the preceding item may repeat. This is where most mistakes live.*Zero or more. Matches even when absent.+One or more.?Zero or one — optional.{3}Exactly three times.{2,4}Between two and four times.{2,}Two or more times, no upper bound.*?Lazy: as few as possible. Add ? to any quantifier.*+Possessive: never gives back. Prevents catastrophic backtracking.JavaScript: Not supported in JavaScript. Use an atomic-group workaround or restructure.
Anchors and boundaries
Positions rather than characters. They match between things, and consume nothing.^Start of the string, or of a line with the m flag.$End of the string, or of a line with the m flag.\bA word boundary — between \w and \W.\BNot a word boundary.\AStart of the string, never of a line — unaffected by the m flag.JavaScript: Not supported. ^ without the m flag is equivalent.
Groups and alternation
Capturing, grouping for precedence, and choosing between alternatives.(…)A capture group. Numbered from 1, left to right by opening bracket.(?:…)A group that does not capture. Use it for precedence alone.(?<name>…)A named capture group.|Alternation. Lowest precedence — group it or it swallows the whole pattern.\1A backreference to what group 1 matched.
Lookaround
Assertions about what surrounds a position, matched without being consumed.(?=…)Positive lookahead: what follows must match.(?!…)Negative lookahead: what follows must not match.(?<=…)Positive lookbehind: what precedes must match.(?<!…)Negative lookbehind: what precedes must not match.
Flags
Modifiers on the whole pattern, written after the closing slash in a literal.gFind every match, not just the first.iCase-insensitive.mMultiline: ^ and $ match at line breaks.sDotall: . also matches a newline.uUnicode mode. Required for \p{…} and for correct handling of astral characters.
Escapes
Characters that need a backslash to be taken literally.\.A literal full stop, not "any character".\\A literal backslash.\n \t \rNewline, tab, carriage return.\u{1F600}A Unicode code point. Needs the u flag.. * + ? ^ $ { } ( ) | [ ] \ /The full set that needs escaping to be literal.
Every example runs in your browser against the same engine as the Regex Tester, so the highlighting is real output rather than a screenshot. To build a pattern from parts, use the Regex Generator.
About the Regex Cheat Sheet
This regex cheat sheet covers every token, quantifier, class, group, anchor, lookaround and flag on one searchable page — and every example runs. Open "Try it" on any row and the pattern executes against its own subject with live match highlighting, using the same engine as the regex tester on this site.
That matters more than it sounds. A cheat sheet whose examples are prose can drift from what the engine actually does, and nobody notices, because nobody runs them. These cannot drift: if an example were wrong, it would visibly fail to highlight. You can also edit both the pattern and the subject in place, which makes the page a scratchpad rather than a poster.
Flavour differences are called out where they exist. Named groups are (?<name>…) in JavaScript, PCRE and .NET but (?P<name>…) in Python; possessive quantifiers exist everywhere except JavaScript; Python's lookbehind must be fixed-width. Switch the flavour selector and the affected rows say what changes.
The sections are ordered by where mistakes actually happen. Quantifiers come second because that is where most regex bugs live — greedy where lazy was meant, or a quantifier bound to one character when it was meant for a group.
- Every token searchable by symbol or by description — search "lookahead" or "(?="
- Live, editable examples on every row, with real match highlighting
- Capture group values shown alongside each match
- Flavour notes for JavaScript, PCRE, Python and .NET where the syntax differs
- Copy any pattern as a /pattern/flags literal
- Grouped by concept rather than alphabetically, so related tokens sit together
How to use it
- Search for what you are looking for — the symbol, or a word from its description.
- Open "Try it" to see the token working against a subject that demonstrates it.
- Edit the pattern or the subject in place to test your own case without leaving the page.
- Switch the flavour selector if you are not writing JavaScript; rows that differ will say so.
- Copy the pattern as a literal when you have what you need.
Real-world use cases
Developers writing regex occasionally
Look up the exact syntax for a lookahead or a named group without leaving the browser tab, when regex is not written often enough to keep the syntax memorised.
Developers switching languages
Check where JavaScript's regex syntax diverges from Python's or PCRE's — named group syntax, possessive quantifiers, lookbehind width restrictions — before porting a pattern between codebases.
Code reviewers
Confirm what an unfamiliar token in a pull request's regex actually does, with a live example to check the reviewer's own understanding before approving it.
Students & developers learning regex
Work through tokens by concept rather than alphabetically, editing the live examples to see cause and effect immediately instead of reading a description and hoping it is right.
Interview & assessment prep
Refresh on quantifier precedence, greedy-versus-lazy matching and anchor behavior — the handful of concepts that account for most regex mistakes — right before they come up in a technical interview.
Examples
Greedy versus lazy
/<.+>/ and /<.+?>/ against <a><b>
Greedy matches <a><b> as one. Lazy matches <a> and <b> separately.
The single most common quantifier bug. Adding ? after any quantifier makes it lazy.
A quantifier binds to one item
/ab+/ against abbb
Matches abbb — the + applies to b alone, not to ab.
Group it: /(?:ab)+/ repeats the pair. Non-capturing, because the grouping is for precedence, not capture.
Alternation has the lowest precedence
/^cat|dog$/ against cow dog
Matches, because it reads as (^cat)|(dog$), not ^(cat|dog)$.
Almost never what was meant. Group the alternatives: /^(cat|dog)$/.
Lookahead matches without consuming
/\d+(?= dollars)/ against 50 dollars and 30 euros
Matches 50, and the word dollars is not part of the match.
That is the whole point of lookaround — assert what surrounds a position without including it in the result.
Common errors
| Message | Cause | Fix |
|---|---|---|
| Invalid regular expression: nothing to repeat | A quantifier with nothing before it — usually a leading * or +, or one immediately after ( or |. | Check for a stray quantifier. If you meant a literal asterisk, escape it as \*. |
| Invalid regular expression: unterminated group | An unbalanced bracket. Escaped brackets inside a character class are a common cause. | Count opening and closing brackets. Paste it into the tester, which reports the position rather than just the fact. |
| The pattern matches too much | A greedy quantifier, or no anchors so the pattern matches anywhere inside a longer string. | Make the quantifier lazy with ?, or anchor with ^ and $. Both are one character. |
| \d matches Arabic-Indic and other non-ASCII digits | In .NET, \d is Unicode-aware by default and matches every digit character in every script. | Use [0-9] where you mean ASCII digits, or RegexOptions.ECMAScript. JavaScript's \d is ASCII-only unless you say otherwise. |
| The browser freezes on a pattern that looks fine | Catastrophic backtracking — nested quantifiers like (a+)+ against a long non-matching string take exponential time. | Avoid nesting quantifiers over overlapping character sets, and anchor where you can. This is a real limitation of the tester on this site too: matching runs on the main thread. |
Frequently asked questions
›Which flavour do the examples use?
JavaScript, because that is what runs in your browser and what the live examples execute against. Almost everything here is identical in PCRE and .NET; the rows that differ carry a note, and switching the flavour selector shows them.
›What is the difference between greedy and lazy quantifiers?
A greedy quantifier takes as much as it can and gives characters back only if the rest of the pattern fails. A lazy one — the same quantifier with ? after it — takes as little as possible and grows only as needed. For anything delimited, such as tags or quoted strings, lazy is almost always what you want.
›When should I use a non-capturing group?
Whenever the group is for precedence rather than for extracting something — which is most of the time. (?:…) keeps the group numbering clean, so your capture groups stay at the indices you expect when you add a grouping later.
›Is lookbehind safe to use now?
In JavaScript, yes — every current browser and Node supports it, including variable-length lookbehind, which most engines do not. Python restricts it to fixed width. If you are targeting Safari before 16.4, check first.
›What is the difference between \b and ^?
Both match a position rather than a character, but ^ is the start of the string or line, while \b is any boundary between a word character and a non-word character. \bcat\b matches cat in "the cat sat" but not in "concatenate".
›Why does my regex match an empty string everywhere?
Something in it can match zero characters — usually a * or {0,} on the whole pattern. With the g flag this produces a match at every position. Use + instead of *, or require at least one character somewhere in the pattern.
›Do the examples run on a server?
No, they run in your browser using the platform's own regex engine. That is why the highlighting is real output rather than a stored screenshot, and why you can edit the pattern and subject freely.
›How is this different from just testing a pattern in the regex tester?
The tester is for a pattern you already have in mind, checked against your own input. This page is for the syntax itself — what a token means, organised by concept, searchable, with a working example attached to each one. Reach for the cheat sheet to look something up, and the tester once you know what you are building and want to check it against real data.
Last updated