Regex Cheatsheet — Syntax, Flags and Metacharacters
The constructs you keep re-looking up, in one place, beside an editor that runs them. Copy a token into the tester and see the result instead of trusting the table.
- Quantifiers are greedy by default; add `?` to make them lazy.
- `\b` asserts a word boundary — it matches a position, never a character.
- `\w` includes the underscore and excludes the hyphen, which surprises most people.
This tool runs entirely in your browser. Your patterns and text are not uploaded, stored, or logged.
The constructs worth memorizing
A small core covers most real work: anchors for position, character classes for sets, quantifiers for repetition, groups for structure, alternation for choices, and lookarounds for context. Everything else is a spelling of one of those.
The distinction that pays for itself is capturing versus non-capturing groups. `(?:…)` groups without spending memory and without taking a backreference slot — which is why swapping a capturing group for a non-capturing one can shift every `$1` after it.
Flags change meaning, not just case
`g` turns a search into iteration, which is what makes `exec` stateful across calls and `replace` act on every match. `m` redefines `^` and `$` per line. `s` lets `.` cross a newline. `i` folds case. `u` and `v` change how the pattern is tokenized and enable `\p{…}` property escapes.
Forgetting one flag produces a pattern that reads correctly and behaves differently, which is the hardest class of regex bug to spot by eye. Check the flags first when a pattern that looks right misbehaves.
- The same source string with different flags is a different program.
- `String.prototype.matchAll` requires the `g` flag and throws without it.
- `RegExp.prototype.test` advances `lastIndex` when `g` or `y` is set, so a reused literal is a bug.
The methods, and which to reach for
`test` answers yes or no. `exec` returns one match with groups and positions and is the only method needed for iteration with a stateful regex. `matchAll` is the readable iteration form. `replace` and `split` consume a pattern to produce a new string.
Reaching for the smallest method that answers the question keeps patterns from being written to satisfy the wrong API — a common reason a simple match ends up with stray capture groups in it.
Frequently asked questions
What is the difference between `[^a-z]` and `\W`?
`[^a-z]` is a negated class matching anything outside lowercase ASCII letters, including digits and symbols. `\W` is fixed shorthand for non-word characters: it keeps letters, digits and underscore and excludes everything else.
When should I use a lookahead instead of a capture group?
Use a lookahead when context must be present but must not be consumed — for example requiring a digit later in a password without including that digit in the match.
Is this cheatsheet valid for Python or PCRE?
Mostly, but not entirely. Character classes, anchors and quantifiers carry over; property escapes, named group syntax and some escaping rules differ between engines.