Regex Cheatsheet

Text

Quick reference for JS regex syntax, character classes, anchors, quantifiers and lookarounds, a handy cheat sheet for writing and debugging match patterns.

42 entries
Character classes
10
.Any char except newline
\dDigit 0-9
\DNon-digit
\wWord char
\WNon-word char
\sWhitespace
\SNon-whitespace
[abc]Match a, b, or c
[^abc]Not a, b, or c
[a-z]a through z range
Anchors
4
^Start of string
$End of string
\bWord boundary
\BNon word boundary
Quantifiers
8
*Zero or more
+One or more
?Zero or one
{n}Exactly n times
{n,}n or more
{n,m}Between n and m times
*?Lazy zero or more
+?Lazy one or more
Groups & lookarounds
8
(abc)Capturing group
(?:abc)Non-capturing group
(?<name>abc)Named group
(?=abc)Positive lookahead
(?!abc)Negative lookahead
(?<=abc)Positive lookbehind
(?<!abc)Negative lookbehind
\1Backreference to group 1
Flags
6
gGlobal
iCase insensitive
mMultiline
sDot matches newline
uUnicode mode
ySticky (y flag)
Common patterns
6
^\d{4}-\d{2}-\d{2}$ISO date
^[\w.+-]+@[\w-]+\.[\w.-]+$Simple email
^https?:\/\/\S+$HTTP URL
^\d{3}-\d{2}-\d{4}$US SSN
^#?([a-f\d]{6}|[a-f\d]{3})$Hex color
^\+?[1-9]\d{1,14}$E.164 phone

About Regex Cheatsheet

Writing regex always means recalling what \d, \w, quantifiers, and zero-width assertions mean, and flipping through docs is slow. This tool is a structured regular-expression cheat sheet that groups common syntax — character classes, quantifiers, anchors, groups, and assertions — by topic, with explanations and examples. Everything loads locally with the page and needs no network lookup, so you can reference it while coding and assemble the right pattern fast. For example, the built-in email-validation pattern works straight out of the box for form input checks.

A gap separates a snippet from a usable expression, and closing it is the premise. Snippets usually describe the shape to match while omitting anchors, boundaries and exclusions, so using one directly to validate is often too permissive. Treat it as a starting point, add anchoring from start to end, state the allowed character ranges and a length limit, and validate with counter-examples.

Engine differences surface the moment you copy between languages. Whether dot matches newlines, whether lookbehind exists, what the character class shorthands mean and whether matching is global by default all vary, so the same expression can behave completely differently elsewhere. Establish the target dialect and check boundary behaviour rather than assuming compatible syntax.

Performance is the commonly ignored dimension: an expression that matches is not necessarily one that matches efficiently. Nested quantifiers backtrack exponentially on long non-matching input and a few hundred characters can saturate a process. Think about worst-case input before adopting a snippet, especially when processing arbitrary user text, and switch to explicit character classes or a length cap when needed.

Its boundary with parsers is worth stating: regular expressions suit linear, simply structured text and not nesting. Parsing a markup or data format with regex looks workable at first and inevitably breaks on comments, escapes and nesting. Reach for a real parser for those and reserve regex for field extraction and format validation where it excels.

Finally, folding the snippets into a project convention improves consistency: agree on the expressions for e-mail, phone and date validation and maintain them centrally instead of each module writing a slightly different version. One edit then changes the rule everywhere, review has a clear reference, and quality is easier to hold than with scattered hand-written patterns.

One maintenance premise: once a regular expression enters the codebase few people are willing to touch it, since it reads as impenetrable. Mitigate by adding a line explaining what it matches and why certain cases are excluded, plus a set of counter-example tests. Both cost little and let a modifier six months later judge whether a change is safe.

How It Works Internally

Cheat-sheet snippets omit anchors and boundary conditions for clarity and must not go straight into production: a digit pattern dropped into a long document matches digits anywhere, and email or phone patterns check shape only. Before use, answer two questions — what must match and what must not — and prepare counter-example tests.

InputUsing \d+ to validate "the whole string is digits"
OutputIt matches "abc123"; anchors are required: ^\d+$

Measured on This Page

37 compileEverything except the 5 quantifier entries (*, +, ?, *?, +?) compiles; those cannot stand alone by definition
6 practical patterns passisoDate matches 2026-01-31 and rejects 2026-1-31; email matches a.b+c@x-y.co and rejects a@; hexColour accepts 6 and 3 digits but not 7; e164 accepts +8613800138000 and rejects a leading 0 — all as intended
Lookbehind works(?<=abc) and (?<!abc) are supported in modern V8 (Chrome 62+), no workaround needed
Named groups(?<name>abc) is valid and the value is reachable through groups.name

The right way to verify a cheat sheet is to run every pattern as code, not to eyeball whether it looks plausible. All 42 hold here. Two things worth knowing: quantifier entries such as *, + and ? cannot compile on their own — they are fragments, and the sheet listing them alone is reasonable as long as users understand they must attach to a character or group; and lookbehind is missing entirely before Safari 16.4, so if old Safari is in the audience, prefer a capture group checked manually. emailSimple-style regexes are front-end triage only — full RFC 5322 syntax is far richer, and real verification belongs to a confirmation mail.

How this was measured: All 42 patterns were extracted and compiled with node's new RegExp (after unescaping the source), and the 6 practical ones — ISO date, email, URL, SSN, hex colour and E.164 phone — were each tested against a positive and a negative sample. · Measured 2026-10-31

How to Use

  1. Open "Regex Cheatsheet"
  2. Filter by syntax name or use case
  3. Browse patterns for matching, groups, lookarounds and replacement
  4. Read the example and the compatibility notes per language
  5. Click "Copy" to drop the pattern into your editor

Use Cases

  • Recall syntax — Instantly look up .*? vs .+? when you forget how to write a non-greedy match.
  • Assertion lookup — Check lookahead and lookbehind syntax to match only in a specific context.
  • Class reference — Look up \d, \s, \b and avoid misusing \b inside a character set.
  • Interview prep — Systematically review regex syntax for written tests and technical interviews.
  • Teaching aid — Use the cheat sheet as a shared reference when explaining regex to newcomers.
  • Pattern debugging — Cross-reference a regex pattern's components against the cheat sheet to find the exact syntax error.
  • Migration reference — Update old PCRE patterns to ECMAScript-compatible syntax by checking the supported features per engine.

FAQ

Which regex flavor does the sheet target?

It centers on the most common syntax, close to JavaScript (ECMAScript); most rules also apply to PCRE and Python, with minor engine-specific differences.

Is \b consistent across languages?

The basic meaning of the word boundary \b is the same, but boundary detection for Unicode characters like CJK can differ between engines, so test it.

Can I test my regex here?

This page is a syntax reference; to verify matches as you type, use one of our regex tester tools for live debugging.

What is the difference between greedy and lazy?

A greedy quantifier like * matches as much as possible, while a lazy one like *? matches as little as possible; lazy is common for paired structures like HTML tags to avoid overshooting.

Why do I need two backslashes in a string?

In most languages a backslash must be escaped in string literals, so regex \d is often written as two backslashes plus d in code — mind the difference.

Can I copy a snippet from the cheat sheet straight into production?

Not directly. Snippets omit anchors and boundary conditions for clarity, so a digit pattern dropped into a long document matches digits anywhere, and email or phone patterns only check shape, never existence. Before use, state what must and must not match, add anchors, and prepare counter-example tests.

One expression behaves differently across languages. Why?

Engine defaults disagree: whether dot matches newlines, whether lookbehind is supported, what the character-class shorthands mean, and whether matching is global by default all vary. When moving a pattern between languages, establish the target dialect and check boundary behaviour item by item rather than assuming compatible syntax, and record key patterns together with their test cases.

Does the dot (.) match newlines in regex?

Not by default — . matches any character except line breaks. Enable the s (dotall) flag or use [\s\S] to cross lines. This is the classic "my .* should have matched" failure with multi-line text.