Regex Interactive Cheatsheet
Click "Try it" on any row to load the pattern into the live tester below.
Live Tester
/
/
No matches
Highlighted matches appear here
No entries match your search.
| Pattern | Description | Example | Actions |
|---|---|---|---|
| Character Classes | |||
. | Any character except newline | c.t → "cat", "cut", "c3t" | |
\d | Any digit [0-9] | \d+ → "42", "100" | |
\D | Any non-digit character | \D+ → "abc ", " xyz" | |
\w | Word character [a-zA-Z0-9_] | \w+ → "hello", "world_2" | |
\W | Non-word character | \W → " ", "-", "@" | |
\s | Whitespace (space, tab, newline…) | \s+ → gap between words | |
\S | Non-whitespace character | \S+ → "hello", "world" | |
\t | Horizontal tab character | \t → tab in TSV data | |
\n | Newline character | \n → line break | |
[abc] | Character class — matches a, b, or c | [aeiou] → vowels | |
[^abc] | Negated class — any char except a, b, c | [^aeiou\s] → consonants | |
[a-z] | Character range (a through z) | [a-zA-Z]+ → words | |
| Quantifiers | |||
* | 0 or more (greedy) | ab* → "a", "ab", "abbb" | |
+ | 1 or more (greedy) | ab+ → "ab", "abbb" (not "a") | |
? | 0 or 1 — makes preceding token optional | colou?r → "color", "colour" | |
{n} | Exactly n occurrences | \d{4} → "2025", "1234" | |
{n,m} | Between n and m occurrences | \d{2,4} → "12", "123", "1234" | |
{n,} | n or more occurrences | \d{3,} → "123", "12345" | |
*? | 0 or more (lazy — shortest match) | <.*?> → each HTML tag | |
+? | 1 or more (lazy — shortest match) | ".+?" → quoted strings | |
| Anchors & Boundaries | |||
^ | Start of string (or line with m flag) | ^\d+ → leading digits | |
$ | End of string (or line with m flag) | \d+$ → trailing digits | |
\b | Word boundary (between word and non-word char) | \bcat\b → "cat" not "catch" | |
\B | Non-word boundary (inside a word) | \Bcat\B → "cat" in "concatenate" | |
| Groups & Alternation | |||
(abc) | Capture group — captures matched text | (\d{4})-(\d{2}) → year, month | |
(?:abc) | Non-capturing group — groups without capturing | (?:foo|bar)baz → "foobaz" | |
(?<name>) | Named capture group | (?<yr>\d{4}) → group "yr" | |
a|b | Alternation — matches a or b | cat|dog → "cat" or "dog" | |
\1 | Backreference to capture group 1 | (\w+) \1 → repeated words | |
| Lookahead & Lookbehind | |||
(?=abc) | Positive lookahead — followed by abc | \d+(?= USD) → price before " USD" | |
(?!abc) | Negative lookahead — NOT followed by abc | \d+(?! USD) → numbers not before USD | |
(?<=abc) | Positive lookbehind — preceded by abc | (?<=\$)\d+ → digits after $ | |
(?<!abc) | Negative lookbehind — NOT preceded by abc | (?<!\$)\d+ → digits not after $ | |
| Flags (Modifiers) | |||
g | Global — find all matches (not just first) | /\d+/g → all numbers | |
i | Case-insensitive matching | /hello/i → "Hello", "HELLO" | |
m | Multiline — ^ and $ match each line start/end | ^\d+/m → leading numbers per line | |
s | dotAll — dot (.) matches newlines too | /.+/s → matches across lines | |
u | Unicode — enables full Unicode matching | /\p{L}+/u → Unicode letters | |
| Escaping Special Characters | |||
\. | Literal dot (escape special char with \) | \. → "3.14" (only the dot) | |
\( \) \[ \] | Literal parentheses / brackets | \(\d+\) → "(42)" | |
Common Regex Recipes
Email Address
[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}Matches standard email addresses including subdomains and plus addressing.
HTTP/HTTPS URL
https?:\/\/(?:www\.)?[-\w]+(?:\.[-\w]+)+[^\s]*Matches http and https URLs with optional www and path/query.
US Phone Number
(?:\+1[\s\-]?)?\(?\d{3}\)?[\s\-]?\d{3}[\s\-]?\d{4}Matches US phone numbers in various common formats.
ISO Date (YYYY-MM-DD)
\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])Strictly validates ISO 8601 dates including month/day range checks.
IPv4 Address
\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\bValidates IPv4 addresses including boundary checks (0–255 per octet).
Hex Color Code
#(?:[0-9a-fA-F]{3}){1,2}\bMatches 3-digit and 6-digit hex color codes prefixed with #.
US ZIP Code
\b\d{5}(?:-\d{4})?\bMatches 5-digit ZIP codes and optional ZIP+4 format.
Strong Password
(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*]).{8,}Requires at least one lowercase, uppercase, digit, and special character. Min 8 chars.
Understanding the Regex Cheatsheet
This cheatsheet covers the full JavaScript regex syntax as implemented in modern browsers (V8 engine). JavaScript regex supports all PCRE-compatible features used in most modern languages — Python, Ruby, Java, Go, PHP — with minor differences in syntax for lookbehinds (available in V8 since Node.js 9.11) and named groups.
Key concepts to remember:
- Greedy vs. lazy quantifiers —
*,+, and{n,m}are greedy by default: they match as much as possible. Adding?after them (e.g.*?,+?) makes them lazy — they match as little as possible. This matters when parsing HTML or quoted strings. - Capture groups vs. non-capture groups — use
(...)when you need to extract the matched text (via.exec()or replace with$1). Use(?:...)when you only need grouping for alternation or quantifiers but don’t need the captured value. - Anchors with multiline flag —
^and$match the start/end of the entire string by default. Enable themflag to make them match the start/end of each line. - Lookaheads are zero-width —
(?=foo)and(?!foo)assert a condition without consuming characters, so the matched position is not advanced. This is useful for conditional matching without including the surrounding context in your match.
Related Tools
Test regex patterns → Regex Tester
Format SQL queries → SQL Formatter
Search/replace text → Text Diff Checker
