Regex Cheatsheet — Syntax, Flags and Metacharacters

//
Flags:

Regex Cheatsheet

Quick reference syntax for characters, quantifiers, anchors, flags, and groups.

Character Classes

\dDigit

Matches any digit (0-9)

e.g. 3 in 'Item 3'

\DNon-digit

Matches any character that is not a digit

e.g. B in '9B'

\wWord character

Matches any alphanumeric character or underscore [a-zA-Z0-9_]

e.g. a in 'cat'

\WNon-word character

Matches any character not in [a-zA-Z0-9_]

e.g. @ in 'user@domain'

\sWhitespace

Matches space, tab, newline, carriage return, form feed

e.g. in 'hello world'

\SNon-whitespace

Matches any character that is not whitespace

e.g. h in ' hello'

.Any character

Matches any single character except line terminators (unless 's' flag)

e.g. x, 1, # in 'a.b'

Quantifiers

*Zero or more

Matches 0 or more occurrences of preceding item (greedy)

e.g. bo* matches 'b', 'bo', 'boooo'

+One or more

Matches 1 or more occurrences of preceding item (greedy)

e.g. a+ matches 'a', 'aaaa'

?Zero or one

Matches 0 or 1 occurrence of preceding item (optional)

e.g. colou?r matches 'color' or 'colour'

{n}Exactly n times

Matches exactly n occurrences of preceding item

e.g. \d{4} matches '2024'

{n,}At least n times

Matches n or more occurrences

e.g. \d{2,} matches '12', '12345'

{n,m}Between n and m

Matches between n and m occurrences (inclusive)

e.g. \d{2,4} matches '99', '2024'

*?Lazy zero or more

Matches smallest possible number of occurrences

e.g. <.*?> matches '<tag>' in '<tag><p>'

+?Lazy one or more

Matches smallest possible occurrences (at least 1)

e.g. a+? matches only first 'a' in 'aaa'

Anchors & Boundaries

^Start of string

Asserts position at start of string (or line with 'm' flag)

e.g. ^Hello matches 'Hello world'

$End of string

Asserts position at end of string (or line with 'm' flag)

e.g. world$ matches 'Hello world'

\bWord boundary

Matches at boundary between \w and \W character

e.g. \bcat\b matches 'cat' but not 'scat' or 'catch'

\BNon-word boundary

Matches at any position where \b does not match

e.g. \Bcat\B matches 'scatters'

Groups & Lookarounds

(...)Capture group

Groups items and captures matched substring into $1, $2, etc.

e.g. (https?):// matches 'https'

(?:...)Non-capturing group

Groups items without saving captured substring

e.g. (?:abc)+

(?<name>...)Named capture group

Captures substring into a named group accessible via groups.name

e.g. (?<year>\d{4})

(?=...)Positive lookahead

Asserts that what immediately follows matches pattern

e.g. \d+(?=px) matches 100 in '100px'

(?!...)Negative lookahead

Asserts that what immediately follows does NOT match pattern

e.g. foo(?!bar) matches 'foo' in 'foobaz'

(?<=...)Positive lookbehind

Asserts that what immediately precedes matches pattern

e.g. (?<=\$)\d+ matches 50 in '$50'

(?<!...)Negative lookbehind

Asserts that what immediately precedes does NOT match pattern

e.g. (?<!\$)\d+ matches 50 in '€50'

Character Sets

[abc]Set

Matches any single character listed inside brackets

e.g. [aeiou] matches vowel

[^abc]Negated set

Matches any single character NOT listed inside brackets

e.g. [^0-9] matches non-digit

[a-z]Range

Matches any character in ASCII range a through z

e.g. [a-zA-Z] matches any ASCII letter

[0-9]Digit range

Matches any single digit from 0 to 9

e.g. [0-9]{3} matches '123'

Flags

gGlobal

Don't return after first match; find all matches in input

e.g. /test/g

iIgnore case

Case-insensitive matching (A-Z == a-z)

e.g. /hello/i matches 'HELLO'

mMultiline

^ and $ match beginning and end of lines

e.g. /^line/m

sDotAll

Allows '.' to match newline characters (\n, \r)

e.g. /a.b/s matches 'a\nb'

uUnicode

Treats pattern as a sequence of Unicode code points

e.g. /\u{61}/u matches 'a'

ySticky

Matches only from the index indicated by lastIndex property

e.g. /foo/y

Your regex and test data stay in your browser.
3 lines · 54 bytes
Support project

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.

Related

Support the free tools