本页面有中文版。
Cette page est disponible en français.
Esta página está disponible en español.

Syntax, rules & examples

Regex cheat sheet

Every regex rule on one page, each with a live example you can try, for the players of Regex Hunter and anyone writing patterns.

Characters & escapes

Details →
SyntaxMeaning · example
abcthe characters a, b, c in order
.any character except a line break
\. \* \\a metacharacter matched literally
\t \n \rtab, newline, carriage return

Classes & shorthands

Details →
SyntaxMeaning · example
[abc]one of a, b or c
[^abc]any character except a, b or c
[a-z]a range: any lowercase letter
\d \Da digit / not a digit
\w \Wa word character [A-Za-z0-9_] / not one
\s \Swhitespace / not whitespace
\p{L}any letter in any script (with the u flag)
SyntaxMeaning · example
^start of the text (of a line with m)
$end of the text (of a line with m)
\ba word boundary
\Bnot a word boundary

Quantifiers

Details →
SyntaxMeaning · example
*zero or more
+one or more
?optional: zero or one
{3}exactly 3
{2,5}between 2 and 5
{2,}2 or more
*? +? ??lazy: as few as possible

Groups & backreferences

Details →
SyntaxMeaning · example
(abc)a capturing group
(?:abc)a group that doesn't capture
(?<name>abc)a named capturing group
\1the text group 1 captured
\k<name>the text a named group captured
a|ba or b

Lookaround

Details →
SyntaxMeaning · example
(?=abc)followed by abc (lookahead)
(?!abc)not followed by abc
(?<=abc)preceded by abc (lookbehind)
(?<!abc)not preceded by abc
SyntaxMeaning · example
iignore case
gglobal: every match, not just the first
mmultiline: ^ and $ at every line
sdotall: . also matches line breaks
uunicode: \p{…} and \u{…}
ysticky: match only at lastIndex

Rules & examples

01 How regex works

A regular expression (regex) is a tiny pattern language for describing text. The engine walks your pattern and the input together, left to right, trying to make every part of the pattern line up with characters in the string. If it can, the string matches.

Two questions cover almost everything you do:

match each char

A pattern like cat means “a c, then an a, then a t, adjacent.”

how many

Quantifiers like + and * say how many times the previous thing may repeat.

By default a regex matches if the pattern is found anywhere in the string — it doesn't have to cover the whole thing unless you anchor it.

//
✓cat
✓a cat sat
✓category
✕dog
Edit the pattern — matches highlight live Open in Playground

In the game (JavaScript)

In Regex Hunter you write the body of the pattern — the part between the slashes. The board marks a word caught when your pattern matches anywhere in it (use anchors to be strict).

02 Literal characters

Most characters match themselves. Letters, digits and spaces are all literals: regex matches the text “regex”, 42 matches “42”. Matching is case-sensitive by default, so Cat does not match “cat” (the i flag changes that).

//
✓code
✕CODE
✓decode
✓coder
Edit the pattern — matches highlight live Open in Playground

PracticeAcademy: Hunter basics

03 Escaping metacharacters

Metacharacters are the dozen characters with a special meaning in regex:

. ^ $ * + ? ( ) [ ] { } | \\ /

To match one literally, put a backslash in front of it. \. matches a real dot, \+ a real plus, \\ a real backslash. Inside a character class most of these lose their power and need no escape.

//
✓3.14
✕3x14
✕3,14
Edit the pattern — matches highlight live Open in Playground

PracticeAcademy: Hunter basics · Play: Escaping hunts

04 The dot ( . )

The dot . matches any single character except a line break (unless the s / dotall flag is set). It is the most over-used token in regex — when you really mean “any letter” or “any digit”, a class is safer.

//
✓cat
✓cot
✓c4t
✕ct
✕caat
Edit the pattern — matches highlight live Open in Playground

Here c.t needs exactly one character between the c and t — so “ct” (zero) and “caat” (two) both fail.

PracticeAcademy: Hunter basics

05 Character classes [ ]

Square brackets define a set — match exactly one character from inside it. [aeiou] matches any one vowel. Use a hyphen for a range: [a-z], [0-9], [A-Fa-f0-9] (a hex digit). Lead with ^ to negate: [^0-9] is “any character that is not a digit.”

TokenMeaning
[abc]one of a, b or c
[a-z]any lowercase letter
[0-9a-fA-F]a hexadecimal digit
[^aeiou]anything except a lowercase vowel
[.\-]a literal dot or hyphen (most metachars are literal inside a class)
//
✓C
✓F
✕H
✕c
✕7
Edit the pattern — matches highlight live Open in Playground

Gotchas

A hyphen is literal at the start or end ([-a], [a-]). To include ], escape it ([\]a]): in JavaScript [] is an empty class, so []a] doesn't work. ^ only negates as the first character.

PracticeAcademy: Character classes · Pack: Character castles · Play: Character class hunts

06 Shorthand classes

Shorthand classes are one-letter shortcuts for common sets, like \d for any digit. Each capital letter is the negation of its lowercase one.

TokenMeaning
\d \Da digit [0-9] / a non-digit
\w \Wa word char [A-Za-z0-9_] / a non-word char
\s \Swhitespace (space, tab, newline…) / non-whitespace
\t \n \rtab, newline, carriage return
//
✓order 66
✕no digits here
✓2026
✓a1b2
Edit the pattern — matches highlight live Open in Playground

In the game (JavaScript)

In JavaScript \d and \w are ASCII-only, even with the u flag; use \p{L} or \p{N} for other scripts. \s already matches Unicode spaces. For golf they're much shorter than spelling out a class.

PracticeAcademy: Character classes

07 Anchors & boundaries

Anchors match a position, not a character — they have zero width.

TokenMeaning
^start of the string (or line, with the m flag)
$end of the string (or line, with the m flag)
\ba word boundary — the edge between a \w and a non-\w
\Ba position that is not a word boundary

Wrap a pattern in ^…$ to force it to match the entire string. \bcat\b matches the whole word “cat” but not “category” or “scatter”.

//
✓cat
✓a cat sat
✕category
✕bobcat
✕concatenate
Edit the pattern — matches highlight live Open in Playground

PracticeAcademy: Anchors & boundaries · Pack: Anchor's away · Play: Anchor hunts

08 Quantifiers

A quantifier says how many times the preceding token may repeat.

TokenMeaning
*zero or more
+one or more
?zero or one (optional)
{3}exactly 3
{2,5}between 2 and 5
{2,}2 or more

They attach to the thing right before them — a character, a class, or a group. colou?r makes the u optional (matches “color” and “colour”); (ab)+ repeats the pair.

//
✕a
✓aa
✓aaaa
✕aaaaa
Edit the pattern — matches highlight live Open in Playground

PracticeAcademy: Quantifiers · Pack: Quantifier quarry · Play: Quantifier hunts

09 Greedy, lazy & possessive

By default quantifiers are greedy: they grab as much as possible, then give characters back if the rest of the pattern needs them. Add ? to make one lazy — it takes as little as possible.

Classic example: against <a><b>, the greedy <.*> matches the whole thing, while the lazy <.*?> stops at the first >.

//g
✓<a><b>
✓<one>two<three>
Edit the pattern — matches highlight live Open in Playground

Possessive & atomic (not in JavaScript)

PCRE/Java also offer possessive quantifiers (a*+) and atomic groups ((?>…)) that never give characters back — a guard against catastrophic backtracking. JavaScript has neither; emulate atomic with a lookahead + backreference.

PracticeAcademy: Quantifiers

10 Alternation & grouping

| means “or” and has the lowest precedence — it splits the whole pattern unless you contain it with parentheses. cat|dog matches either word; gr(a|e)y matches “gray” or “grey”, whereas gra|ey would mean “gra” or “ey”.

//
✓gray
✓grey
✕groy
Edit the pattern — matches highlight live Open in Playground

PracticeAcademy: Groups & backreferences · Play: Alternation hunts

11 Groups: capture & name

Parentheses do two jobs: they group tokens so a quantifier or | applies to all of them, and they capture what matched for later reuse.

TokenMeaning
(…)capturing group — numbered 1, 2, 3… left to right
(?:…)non-capturing group — groups without the bookkeeping
(?<name>…)named capturing group

Use (?:…) when you only need grouping (it's faster and keeps your group numbers tidy), and names when a pattern has several captures worth labelling.

//
✓ab
✓abab
✓ababab
✓aba
Edit the pattern — matches highlight live Open in Playground

PracticeAcademy: Groups & backreferences · Pack: Tricky patterns · Play: Group hunts

12 Backreferences

A backreference matches the same text a group already captured. \1 refers to group 1, \2 to group 2, \k<name> to a named group. This is how you require repetition:

TokenMeaning
(.)\1any character followed by itself — a double letter
(.)(.)\2\1a 4-char palindrome (e.g. “abba”)
(.).*\1same character appears again later
//
✓book
✓tree
✕cat
✓success
Edit the pattern — matches highlight live Open in Playground

Backref vs. class

Crucially, (.)\1 requires the same character twice — that's different from [a-z][a-z] (any two letters). Backreferences are what let regex check “the same thing again,” which a plain class can't express.

PracticeAcademy: Groups & backreferences

13 Lookahead & lookbehind

Lookaround asserts that something does (or doesn't) appear next to the current spot without consuming any characters — it's a zero-width condition.

TokenMeaning
(?=…)positive lookahead — followed by …
(?!…)negative lookahead — not followed by …
(?<=…)positive lookbehind — preceded by …
(?<!…)negative lookbehind — not preceded by …

A password-style rule “contains a digit and a letter” stacks lookaheads at the start: ^(?=.*\d)(?=.*[a-z]).+$. Each (?=…) checks the whole string from position 0, then the match continues. Lookbehind requires a fixed (or bounded) width in most engines.

//g
✓16px
✓24 px
✕16pt
✕px
Edit the pattern — matches highlight live Open in Playground

In the game (JavaScript)

JavaScript has supported lookbehind since ES2018 — modern browsers run it fine, so it works in the game. Older engines (and some tools) lack it.

PracticeAcademy: Lookarounds · Play: Lookaround hunts

14 Flags

Flags are single letters that change how the whole pattern matches, like i for case-insensitive. In code they sit after the closing slash (/cat/gi).

TokenMeaning
iignore case
gglobal — find all matches, not just the first
mmultiline — ^ and $ match at every line break
sdotall — . also matches newlines
uunicode mode (enables \u{…} and \p{…})
ysticky — match only at lastIndex
xextended/verbose: ignore whitespace & allow comments (PCRE/Python; not JS)
//gi
✓Cat caught a CAT
✓category
Edit the pattern — matches highlight live Open in Playground

15 Unicode & properties

With the u flag you can match by Unicode property: \p{L} is any letter in any script, \p{N} any number, \p{Emoji} emoji, and \P{…} negates. \u{1F600} matches a code point by hex value.

Without u, a regex works on UTF-16 code units, so an emoji or other astral character counts as two “characters” — usually not what you want. Property escapes are the clean way to handle real-world text.

Engine support

Property escapes exist in JavaScript (with u), PCRE, Python (via the regex module), Java and others, but the exact property names differ. Stick to the common ones (L, N, P, script names) for portability.

16 Common patterns

These are short, reusable patterns for everyday formats: integers, decimals, emails, hex colours and ISO dates. Treat “validation” regexes as approximations — real email/URL rules are far hairier than a one-liner.

TokenMeaning
^\d+$a whole non-negative integer
-?\d+(\.\d+)?an optional-sign decimal number
^[\w.+-]+@[\w-]+\.[\w.-]+$a rough email shape
^#?[0-9a-fA-F]{6}$a 6-digit hex colour
^\d{4}-\d{2}-\d{2}$an ISO date (YYYY-MM-DD)
//
✓42
✓-3.14
✕3.
✕x9
Edit the pattern — matches highlight live Open in Playground

PracticeAcademy: Real-world validators · Play: Mixed hunts

17 Tips for shorter patterns

Golf is the art of the shortest pattern that catches every target and spares every decoy. The board only asks “does it match anywhere” — so you rarely need anchors or to describe the whole word.

  • Find the smallest distinguishing feature. If every target contains q and no decoy does, the answer is just q.
  • Prefer a class over alternation: [cb]at beats cat|bat.
  • Shorthands are short: \d over [0-9], \w over [A-Za-z0-9_].
  • Drop anchors unless a decoy forces them. Add ^ or $ only to exclude a near-miss.
  • Make differences optional with ? instead of branching.
  • Reach for a backreference when targets share a repeat the decoys lack.

Try shrinking this — both cat and bat are targets, “rat” is a decoy:

//
✓cat
✓bat
✕rat
✕mat
Edit the pattern — matches highlight live Open in Playground

Ready to compete? The daily hunt, practice packs and the playground are where to put this into practice.

PracticeAcademy: Golf techniques

18 Flavor differences

Regex flavors are the dialects of different regex engines, and they don't all support the same features. The big families are JavaScript, PCRE (PHP, and close to Perl), Python (re / regex) and Java.

TokenMeaning
LookbehindJS (ES2018+), PCRE, Python, Java — fixed-width in most; PCRE/Python allow more
Possessive / atomicPCRE, Java, Python regex — not JavaScript
Recursion (?R)PCRE & Python regex only — no JS, no Python re
Named group(?<n>) in JS/PCRE/.NET; (?P<n>) in classic Python
x / verbose flagPCRE, Python, Java — not JavaScript

In the game (JavaScript)

The game evaluates your pattern with the browser's JavaScript engine, so JS rules apply: lookbehind and named groups work; possessive quantifiers and recursion don't. The live testers above run the same engine.

19 Performance & backtracking

Most regexes are fast, but nested quantifiers over the same characters can explode. (a+)+$ against a long run of “a” followed by “!” forces the engine to try an exponential number of splits — catastrophic backtracking.

  • Avoid a quantifier inside a quantifier that can match the same thing ((a+)+, (.*)*).
  • Make inner pieces mutually exclusive so there's only one way to match.
  • Anchor with ^/$ and prefer specific classes over . to cut the search space.
  • In engines that have them, possessive quantifiers / atomic groups stop the backtracking entirely.