Regex Cheatsheet

By JXXR1

Quick reference: anchors, character classes, quantifiers, groups, lookarounds, flags. Click any pattern to copy.

Click any pattern in the table to copy it. Examples are illustrative.

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

Browse, filter, click to copy

Greedy vs lazy, flavour gaps, and the m/s flag trap

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.

Named capture groups for readable extraction

Use (?<year>\d{4}) instead of a bare (\d{4}) when extracting structured data. Named groups make the intent clear in code review and survive refactoring — reordering groups doesn’t break downstream references because you access results by name, not by index. JavaScript, Python, and PCRE all support the (?<name>...) syntax.

Atomic groups and backtracking control

Catastrophic backtracking happens when a quantifier-heavy pattern matches many possible ways and the engine tries every permutation before failing. PCRE’s atomic groups (?>...) and possessive quantifiers (*+, ++) prevent this by locking the match once taken. JavaScript lacks both constructs, so the defensive approach there is to avoid nested quantifiers like (a+)+ entirely and use character-class negation instead of open-ended .* patterns.