Regex Cheatsheet
Quick reference: anchors, character classes, quantifiers, groups, lookarounds, flags. Click any pattern to copy.
What is this for?
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.
When to use it
- 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 — common gotchas are listed below.
- You're switching between regex flavours (JavaScript, PCRE, Python) and need to check what's available in your target environment.
How to use it
- 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.
Common gotchas
- 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[].