Regex Cheat Sheet

Regular expression syntax reference with a live tester: click any token to see what it matches.

A quick reference for regular expression syntax, with every row ready to try. The sheet covers character classes, anchors and word boundaries, quantifiers (greedy and lazy), groups and back-references, lookahead and lookbehind, escapes and common ready-made patterns, plus the flags. Click any row to load it into the tester at the top and see what it matches in sample text you can edit. The syntax shown is JavaScript's, which matches PCRE, Python and Java for nearly everything here.

Try it: click any row below to load it here

//
<b>bold</b> and <i>italic</i>

*? +? ??: Lazy: match as little as possible

Character classes

Any character except a newline (with the s flag, newlines too)
A digit, 0–9
Anything except a digit
A word character: letter, digit or underscore
Anything except a word character
Whitespace: space, tab, newline
Anything except whitespace
Any one of a, b or c
Any character except a, b or c
Any character in the range a to z
Any Unicode letter (needs the u flag)

Anchors and boundaries

Start of the string (start of each line with the m flag)
End of the string (end of each line with the m flag)
Word boundary
Not a word boundary

Quantifiers

0 or more
1 or more
0 or 1 (optional)
Exactly n times
n or more times
Between n and m times
Lazy: match as little as possible

Groups and references

Capturing group
Non-capturing group
Named capturing group
Back-reference to group 1
Alternation: a or b

Lookaround

Lookahead: followed by abc
Negative lookahead: not followed by abc
Lookbehind: preceded by abc
Negative lookbehind: not preceded by abc

Escapes

A literal special character
Tab, newline, carriage return
A Unicode character by code (\u{1F600} with the u flag)

Common patterns

Good enough for forms; full RFC 5322 is far longer
http or https links
Four numbers 0–255 separated by dots
ISO 8601 date
HH:MM
#RGB or #RRGGBB
5 digits, optional +4
8+ chars with lower, upper, digit and symbol
Leading or trailing spaces
The same word twice in a row

Flags

gGlobal: find all matches, not just the first
iIgnore case
mMultiline: ^ and $ match at line breaks
sDot-all: . also matches newlines
uUnicode: enables \p{…} and \u{…}, treats surrogate pairs as one character
ySticky: match only at lastIndex
dIndices: report start and end positions of groups
vUnicode sets: set operations in classes, e.g. [\p{L}--[a-z]]

Syntax shown is JavaScript's, which matches PCRE, Python and Java for nearly everything here. Lookbehind and \p{…} need a modern engine.

Common use cases

  • Look up the syntax for a quantifier, group or lookaround
  • Find a ready-made pattern for emails, dates or numbers
  • Learn regex by trying each token on sample text
  • Remind yourself what \b, \s or (?:…) mean

How to use

  1. Browse the sections, or search the page with Ctrl+F (Cmd+F on a Mac).
  2. Click a row to load its pattern into the tester.
  3. Edit the pattern, flags or test text to experiment.
  4. Move to the regex tester for longer tests and replacements.

FAQ

What's the difference between greedy and lazy?

Greedy quantifiers (*, +) match as much as possible; adding ? makes them lazy (*?, +?), matching as little as possible. <.+> matches a whole line of tags; <.+?> matches one tag.

Why do I need to escape some characters?

. * + ? ^ $ ( ) [ ] { } | \ and / have special meanings. Put a backslash before them to match the character itself, or use the regex escape tool.

Do these work in Excel or Google Sheets?

Google Sheets' REGEXMATCH uses RE2, which supports most of this but not lookarounds or back-references. Excel's newer REGEX functions use PCRE2.