DEVELOPER REFERENCE

Regex Cheat Sheet: Patterns, Flags, and Safer Testing

This quick reference targets JavaScript regex syntax. A pattern match does not prove that input is real or trustworthy; email, identity, URL, and security checks still need domain rules and server-side validation.

Test in the live tool
PRACTICAL METHOD

From syntax to a dependable result

01

Write the validation goal first

Before writing a regex, describe the accepted shape and rejection boundaries in plain sentences. For phone, email, or identity-like fields, character shape is only one layer; length, country or institutional rules, and ownership checks remain separate.

02

Test small and opposing fixtures

Prepare at least one expected match, one expected rejection, and one long or malformed input. With the global flag, inspect all matches and lastIndex behavior; for multiline text, test the effects of m and s independently.

03

Acceptance check before production

Run the pattern again in the target runtime and include Unicode characters and unexpected line endings. Bound user-supplied patterns, and never treat a pattern as a security boundary until nested quantifiers have been measured against adversarial long input.

04

Know when a parser is the right tool

One regex is not a dependable parser for nested JSON, HTML, programming languages, free-form postal addresses, or an entire email standard. Use the relevant parser or domain library first and reserve regex for a narrow, disclosed pre-check.

01

Core building blocks

SyntaxMeaningExample
.Any single character except a line breaka.c
\d / \DDigit / non-digit\d{4}
\w / \WASCII word character / inverse\w+
[a-z] / [^a-z]Range / negated character class[A-F0-9]
^ / $Start / end of input or line^OK$
02

Repetition and grouping

SyntaxMeaningExample
* / + / ?Zero-or-more / one-or-more / optionalhttps?
{n} / {n,m}Exact or ranged repetition\d{2,4}
(abc) / (?:abc)Capturing / non-capturing group(?:png|webp)
(?<name>...)Named capture group(?<year>\d{4})
(?=...) / (?!...)Positive / negative lookahead\d+(?=px)
03

JavaScript flags

SyntaxMeaningExample
gFind all matches rather than stopping at the first
iCase-insensitive matching
mMake ^ and $ operate per line
sAllow . to match line breaks
u / vUnicode modes; check engine support
FAQ

Common questions

Can regex validate an email address?+

Regex checks only a format candidate. Deliverability and ownership require a verification email.

What is ReDoS risk?+

Poorly designed nested quantifiers can cause extreme backtracking. Bound input, test patterns, and isolate untrusted expressions.