JavaScript Regex Cheatsheet

A complete reference guide for regular expressions with an interactive live tester.

Live Regex Tester No matches yet
/ /

Anchors & Boundaries

Pattern Description Example
^ Start of string / line ^Hello matches 'Hello'
$ End of string / line world$ matches 'world'
\b Word boundary \bcat\b matches 'cat'
\B Non-word boundary \Bcat matches 'scatter'
\A Start of string (Use ^) Not supported in JS
\Z End of string (Use $) Not supported in JS

Character Classes

Pattern Description Example
. Any char except newline a.c matches 'abc'
\d / \D Digit / Non-digit \d+ matches '123'
\w / \W Word / Non-word char \w+ matches 'hello_42'
\s / \S Whitespace / Non-space \s+ matches spaces
[abc] Character set [aeiou] matches vowels
[^abc] Negated set [^0-9] matches non-digits
[a-z] Character range [a-zA-Z] matches letters
[\u0041-\u005A] Unicode range Matches A-Z via Unicode

Quantifiers

Pattern Description Example
* Zero or more (greedy) a* matches '', 'a', 'aaa'
+ One or more (greedy) a+ matches 'a', 'aaa'
? Zero or one (optional) colou?r matches 'color'
{n} Exactly n occurrences \d{4} matches '2024'
{n,} n or more occurrences \d{2,} matches '123'
{n,m} Between n and m times \d{2,4} matches '123'
*? / +? / ?? Lazy quantifiers Stops early

Groups & Lookarounds

Pattern Description Example
(abc) Capturing group (\d+) captures digits
(?:abc) Non-capturing group Groups without capture
(?<name>abc) Named capturing group (?<year>\d{4})
\1 / \k<name> Backreferences (\w)\1 matches 'aa'
(?=abc) / (?!abc) Positive / Negative lookahead \d(?=px) matches digit
(?<=abc) / (?<!abc) Positive / Negative lookbehind (?<=\$)\d+ after $
a|b Alternation (OR) cat|dog matches either

JavaScript Flags

Pattern Description Example
g Global search Find all matches
i Case-insensitive Ignore case differences
m Multiline mode ^ and $ line starts
s Dotall mode . matches newlines too
u Unicode mode Enables full Unicode
y Sticky mode Match from lastIndex
d Indices ES2022: match.indices

Escape Sequences

Pattern Description Example
\n / \r / \t Newline, Return, Tab Formatting characters
\0 Null character Binary data delimiter
\uXXXX / \u{...} Unicode escapes \u0041 matches 'A'
\xHH Hex escape (2 digits) \x41 matches 'A'
\\. / \. Literal backslash / dot Escaping special symbols

Common Regular Expression Patterns

Email Address (Basic)
^[^\s@]+@[^\s@]+\.[^\s@]+$

Example match: user@example.com

URL (HTTP/HTTPS)
https?:\/\/[\w\-._~:/?#[\]@!$&'()*+,;=%]+

Example match: https://example.com

IPv4 Address
\b(?:\d{1,3}\.){3}\d{1,3}\b

Example match: 192.168.1.1

ISO Date (YYYY-MM-DD)
^\d{4}-\d{2}-\d{2}$

Example match: 2026-06-06

Hex Color Code
^#?(?:[0-9a-fA-F]{3}){1,2}$

Example match: #FF6B35 or #f60

Strong Password Validation
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$

Example match: Password1

UUID v4
^[0-9a-fA-F]{8}-(?:[0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}$

Example match: 550e8400-e29b-41d4-a716-446655440000

Trim Whitespace
^\s+|\s+$

Use with .replace() to trim spaces

About JavaScript Regular Expressions

Mastering pattern matching for data validation, parsing, and text search operations.

ECMAScript Compliant

Regular expressions (Regex) provide a powerful and concise syntax for matching patterns in strings. In JavaScript, regex objects can be created using literal notation /pattern/flags or the RegExp constructor for runtime pattern construction.

Matching

Pattern Validation

Validate form inputs, phone numbers, emails, and postal codes efficiently before submitting data.

Parsing

String Parsing & Extraction

Extract specific tokens, URLs, parameters, and structured data blocks from complex text logs.

Private

100% Client-Side Testing

Test patterns instantly in your browser sandbox without sending string data across the network.

Speed

Instant Feedback

Matches highlight immediately as you edit expressions or update test strings in the sandbox.

Reference

Quick Lookup Cards

Easily reference character classes, anchors, and quantifiers right when you need them.

Utility

Common Templates

Jumpstart development with pre-tested patterns for emails, URLs, and numeric identifiers.

UtilityGlen

Handy web tools for daily use. Everything runs right in your browser—no sign-ups, no cloud uploads, and your data stays entirely on your device.