Regex Cheatsheet
Quick reference: anchors, character classes, quantifiers, groups, lookarounds, flags. Click any pattern to copy.
Anchors, classes, quantifiers — the patterns you actually copy-paste
Regex syntax is dense and easy to half-remember. This is a quick, searchable reference for the patterns you need: anchors, character classes, quantifiers, groups, lookarounds, and flags. Rather than hunt through documentation or Stack Overflow, click any pattern to copy it straight into your editor, or use the live filter to find what you're looking for in seconds.
Lookbehinds, flags, starter patterns — six reasons to keep this open
- You need to write a regex but can't recall the exact syntax — like whether a lookbehind is
(?<=foo)or(?=foo), or how to make^match line starts instead of string start. - You're validating or parsing something (email shape, ISO date, UUID, phone number) and want a tested starter pattern you can copy and adapt rather than write from scratch.
- You're explaining regex to a colleague or in documentation and need a stable, printable reference rather than browser tabs.
- You need to know which flag does what — particularly the confusing pair:
m(multiline, affects anchors) versuss(dotall, makes.match newlines). - You've written a regex that feels wrong (matches too much, too little, or in the wrong places) and want to understand why — pitfalls are listed below.
- You're switching between regex flavours (JavaScript, PCRE, Python) and need to check what's available in your target environment.
Browse, filter, click to copy
- Browse the tables. Patterns are grouped by category: anchors anchor your match to positions; character classes match sets of characters; quantifiers control how many times; groups bundle and capture; lookarounds assert without consuming.
- Use the filter. Type a keyword (e.g. "email", "word", "lookahead") to narrow the view. Most developers know roughly what they want; the filter cuts the noise.
- Click to copy. Any pattern in the table can be clicked to copy it to your clipboard. Paste it into your code and modify as needed.
- Test it. This cheatsheet shows syntax; the Regex Tester lets you run patterns against real text and see what matches. Always test against your actual data before deploying.
Greedy vs lazy, flavour gaps, and the m/s flag trap
- Flavour differences. Most patterns here work in JavaScript and PCRE, but features vary. Lookbehind syntax only arrived in JavaScript with ES2018. The
x(extended) flag exists in PCRE and Python, not JavaScript. Possessive quantifiers like++are PCRE-only. Check your target environment. mflag confusion. In multiline mode,^and$match the start and end of each line, not the whole string. To make.match newlines (so.*can cross line boundaries), uses(dotall), notm.- Greedy matching eats too much.
<.*>against<a>b</a>matches the entire string, not just<a>. Use a lazy quantifier<.*?>or, better, a negated class<[^>]+>to be explicit about what you don't want. - Regex is not a parser. The email, URL, and JSON patterns here are shape validators, not correctness checkers. For production code, parse structured data with a proper parser; use regex only for one-off string validation or extraction.
- Character class negation looks like lookahead.
[^abc]means "not a, b, or c" (negated class).(?!abc)means "not followed by abc" (negative lookahead). They're different; the caret^only negates inside[].
JavaScript / PCRE flavour notes
The tables group the syntax the way you actually reach for it: Anchors, Character classes, Quantifiers, Groups & alternation, Lookarounds, Flags, and a set of Common patterns. Click any pattern cell to copy it straight to your clipboard, and use the filter box to jump to a token by name — type lookahead, digit, or ?= and the sheet narrows as you go. The example column is illustrative: it shows what a token does, not a drop-in, fully-anchored validator.
The flavour is JavaScript / PCRE, and the notes column flags where they diverge so you don't copy something your engine rejects. \A and \Z (absolute string anchors) and possessive quantifiers like *+ are PCRE-only — JavaScript has neither. The free-spacing x flag is a PCRE/Python convenience, not a JS one. When a row is marked "PCRE only", assume it will silently misbehave or throw in a browser.
Lazy quantifiers, lookaround direction, and escaping for your language
How do I make a quantifier lazy instead of greedy? Append ? to it: *?, +?, ??, {2,5}?. Greedy .* grabs as much as possible then backtracks; lazy .*? grabs as little as possible — the usual fix for a pattern that "matches too much" between two delimiters.
Lookahead vs lookbehind? Lookahead asserts what follows — (?=…) positive, (?!…) negative — and lookbehind asserts what precedes — (?<=…) positive, (?<!…) negative. Both are zero-width: they test without consuming characters. Lookbehind works in modern JavaScript (ES2018+) but is absent or length-restricted in some older engines.
Why does my copied pattern not match? The examples are teaching snippets, not anchored validators — a real match usually needs your own anchors (^…$), the right flags (g, i, m, s), and correct escaping for the host language's string rules. Copy the token, then build the full expression around it.
What do the flags do? g finds all matches (not just the first), i is case-insensitive, m makes ^/$ match at line breaks, and s (dotall) lets . match newlines. They stack — /…/gim is common.