Regular Expressions
Mental model
Section titled “Mental model”A regex is a small program run by a backtracking engine: it tries the pattern from each start position, and when a step fails it rewinds to the last choice point and tries the next option. Most surprises follow from that: greedy quantifiers grab too much, unanchored patterns match inside strings, and nested choices can explode into billions of retries. Write patterns that are anchored and fail fast.
Literals and character classes
Section titled “Literals and character classes”| Syntax | Matches |
|---|---|
abc | The literal text abc |
. | Any char except newline |
[abc], [a-z] | One char from the set |
[^0-9] | One char not in the set |
\d, \w, \s | Digit, word char, whitespace |
\D, \W, \S | Their negations |
Escape the metacharacters . * + ? ^ $ ( ) [ ] { } | \ with a
backslash to match them literally. Inside a class most of them are
literal already; only ], \, ^ (first) and - (between chars) need
care.
/gr[ae]y/.test("grey"); // true/\d\d:\d\d/.exec("at 09:45")[0]; // "09:45"/3.14/.test("3x14"); // true: . is any/3\.14/.test("3x14"); // falseRegExp(RegExp.escape("1+1")).test("1+1"); // trueTip: Never build a pattern from user input by concatenation. Use
RegExp.escape()(ES2025, Node 24+),re.escape()in Python orRegex.Escape()in .NET.
Quantifiers and greediness
Section titled “Quantifiers and greediness”| Syntax | Means | Lazy form |
|---|---|---|
* | 0 or more | *? |
+ | 1 or more | +? |
? | 0 or 1 | ??nullish coalescing operator question question |
{3} | Exactly 3 | — |
{2,5} | 2 to 5 | {2,5}? |
{2,} | 2 or more | {2,}? |
Quantifiers are greedy: they take as much as possible, then give back one character at a time until the rest of the pattern fits. The lazy form takes as little as possible and grows. A negated class is often clearer and faster than either.
const s = "<b>bold</b>";s.match(/<.+>/)[0]; // "<b>bold</b>" greedys.match(/<.+?>/)[0]; // "<b>" lazys.match(/<[^>]+>/)[0]; // "<b>" no backtracking"aaa".match(/a{2}/)[0]; // "aa"Gotcha:
.*stops at a newline, so it silently fails on multi-line input. Add thesflag, or match[\s\S]*.
Anchors and boundaries
Section titled “Anchors and boundaries”| Syntax | Matches at |
|---|---|
^ | Start of input (line with m) |
$ | End of input (line with m) |
\b | Word/non-word boundary |
\B | Not a word boundary |
\A, \z | Absolute start/end (Python, .NET) |
Without anchors, test succeeds if the pattern matches anywhere in the
string, which is almost never what validation means. Anchor both ends.
/\d{4}/.test("abc12345"); // true!/^\d{4}$/.test("abc12345"); // false/^\d{4}$/.test("2026"); // true"cat scat".match(/\bcat\b/g); // ["cat"]"a\nb".match(/^b$/m)?.[0]; // "b"Gotcha: In Python and .NET,
$also matches before a trailing newline, so"42\n"passes^\d+$. Use\z(Python 3.14+, .NET) or Python’sre.fullmatch()to reject it.
Groups and backreferences
Section titled “Groups and backreferences”| Syntax | Does |
|---|---|
(abc) | Capture group 1 |
(?:abc) | Group without capturing |
(?<y>\d+) | Named group (Python: (?P<y>…)) |
\1, \k<y> | Match the same text again |
a|b | Alternation, lowest precedence |
Alternation binds loosest: ^cat|dog$ means ^cat or dog$. Wrap it
in a non-capturing group to scope it. Named groups survive edits that
renumber positional ones.
const re = /(?<y>\d{4})-(?<m>\d{2})/;const { y, m } = "on 2026-11".match(re).groups;[y, m]; // ["2026", "11"]/^(?:cat|dog)$/.test("catdog"); // false/^cat|dog$/.test("catdog"); // true!/(\w)\1/.exec("hello")[0]; // "ll"Gotcha: Python’s
rerejects(?<y>…)with “unknown extension”. It needs(?P<y>…), and(?P=y)to refer back to it.
Lookaround
Section titled “Lookaround”| Syntax | Asserts the text ahead/behind |
|---|---|
(?=x) | Is followed by x |
(?!x) | Is not followed by x |
(?<=x) | Is preceded by x |
(?<!x) | Is not preceded by x |
Lookarounds check a condition without consuming characters, so the match itself excludes them. They are the clean way to express “every rule must hold” for things like password policies.
"$42 and €7".match(/(?<=\$)\d+/)[0]; // "42""price: 100px".match(/\d+(?=px)/)[0]; // "100"const strong = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{12,}$/;strong.test("Correct8Horse"); // truestrong.test("correcthorse"); // falseGotcha: Python’s lookbehind must have a fixed width:
(?<=\$|USD)raisesre.error. JavaScript and .NET accept variable-length lookbehind.
Flags and Unicode
Section titled “Flags and Unicode”| JS flag | Python / .NET | Effect |
|---|---|---|
i | re.I / IgnoreCase | Case-insensitive |
m | re.M / Multiline | ^/$ per line |
s | re.S / Singleline | . matches newline |
g | findall / Matches | Find every match |
u, v | always on for Python str | Code points, \p{…} |
(?i:x) | same inline syntax | Flag for a group only |
Without u or v, JavaScript sees UTF-16 code units, so an emoji is
two “characters”. v (ES2024) adds set operations to u. Inline
modifiers like (?i:x) are ES2025 and Baseline since 2025.
/^.$/.test("😀"); // false: 2 units/^.$/u.test("😀"); // true/\p{L}+/u.exec("añb1")[0]; // "añb"/[\p{L}--[a-z]]/v.test("q"); // false/(?i:a)b/.test("Ab"); // trueGotcha: A
gregex keepslastIndexbetween calls, so the same object alternates:re.test("a")istrue, thenfalse. Dropgfortest, or create the regex per call.
Replacement syntax
Section titled “Replacement syntax”| Insert | JavaScript | Python / .NET |
|---|---|---|
| Group 1 | $1 | \1 / $1 |
| Named group | $<y> | \g<y> / ${y} |
| Whole match | $& | \g<0> / $& |
Literal $ | $$ | $ / $$ |
| Computed | callback (m, …g) => | function / MatchEvaluator |
Replacement strings have their own mini-language, separate from the pattern. Pass a function when the replacement depends on the match.
"2026-11-02".replace( /(\d+)-(\d+)-(\d+)/, "$3.$2.$1"); // "02.11.2026""a-b-c".replace(/-/g, "+"); // "a+b+c""x1y22".replace(/\d+/g, (d) => d * 2); // "x2y44""a.b".replaceAll(".", "!"); // "a!b"import rere.sub(r"(?P<y>\d{4})-(\d\d)", r"\2/\g<y>", "2026-11") # '11/2026're.fullmatch(r"\d+", "42x") # NoneGotcha: JavaScript’s
replacewith a string or a regex withoutgchanges only the first match.replaceAllwith a regex that lacksgthrows aTypeError.
Catastrophic backtracking
Section titled “Catastrophic backtracking”| Risky shape | Why | Safer |
|---|---|---|
(a+)+ | Nested quantifiers | a+ |
(a|a)* | Overlapping choices | a* |
(\w+\s?)+$ | Ambiguous split | Atomic or possessive |
.*x.*y on long input | Many retry points | [^x]*x[^y]*y |
When a match fails, the engine retries every way the input could be
split among nested or overlapping quantifiers: exponential in input
length. One crafted string can pin a CPU core (ReDoS). Python 3.11+
has atomic groups (?>…) and possessive *+; .NET has atomic groups
and RegexOptions.NonBacktracking; JavaScript has neither.
const bad = /^(a+)+$/;const s = "a".repeat(26) + "!";bad.test(s); // ~0.5 s; doubles per extra "a"/^a+$/.test(s); // false, instantlyWarning: Run untrusted input only against linear-shaped patterns, and set a timeout where the engine supports one. .NET’s default
matchTimeoutis infinite.
Dialects and testing
Section titled “Dialects and testing”| Feature | JavaScript | Python re · .NET |
|---|---|---|
\d matches | ASCII 0-9 | Any Unicode digit · same |
\p{L} | With u or v | No (regex pkg) · yes |
| Variable lookbehind | Yes | No · yes |
Atomic (?>…) | No | 3.11+ · yes |
| Whole-string match | ^…$ | fullmatch · ^…\z |
Test a pattern against inputs that should fail, not just ones that should pass: empty string, trailing newline, non-ASCII digits, and a long near-miss for backtracking. Keep those cases in a unit test next to the pattern.
import rere.search(r"^\d+$", "٤٢") # matches!re.search(r"^\d+$", "٤٢", re.A) # Nonere.prefixmatch(r"ab", "abc") # 3.15 nameNote: Python 3.15 adds
re.prefixmatch()as the clearer name forre.match(), which only matches at the start of the string;matchstays as a soft-deprecated alias.
Further reading
Section titled “Further reading”A version typeset for e-ink is available at https://sokurenko.dev/languages/regex/