Reference
Regex Cheat Sheet
Quick reference for JavaScript regular expression syntax. Test patterns live with the Regex Tester.
Character Classes
| Pattern | Description | Example |
|---|
| . | Any character except newline (unless s flag is set) | a.c → abc, a1c, a-c |
| \d | Any digit [0-9] | \d{3} → 123, 456 |
| \D | Any non-digit [^0-9] | \D+ → abc, --- |
| \w | Any word character [A-Za-z0-9_] | \w+ → hello, var_1 |
| \W | Any non-word character [^A-Za-z0-9_] | \W → @, #, ! |
| \s | Any whitespace (space, tab, newline) | \s+ → matches spaces |
| \S | Any non-whitespace character | \S+ → hello |
| [abc] | Character set — matches a, b, or c | [aeiou] → vowels |
| [^abc] | Negated set — any character except a, b, c | [^0-9] → non-digits |
| [a-z] | Character range — a through z | [A-Za-z] → letters |
Quantifiers
| Pattern | Description | Example |
|---|
| * | Zero or more of the preceding element | ab*c → ac, abc, abbc |
| + | One or more of the preceding element | ab+c → abc, abbc |
| ? | Zero or one of the preceding element (optional) | colou?r → color, colour |
| {n} | Exactly n of the preceding element | \d{4} → 2024 |
| {n,} | n or more of the preceding element | \d{2,} → 12, 123, 1234 |
| {n,m} | Between n and m of the preceding element | \d{2,4} → 12, 123, 1234 |
| *? | Lazy zero or more (shortest match) | <.*?> → matches one tag |
| +? | Lazy one or more (shortest match) | ".+?" → "one" not "one" "two" |
Anchors & Boundaries
| Pattern | Description | Example |
|---|
| ^ | Start of string (or line with m flag) | ^Hello → matches start |
| $ | End of string (or line with m flag) | end$ → matches at end |
| \b | Word boundary | \bword\b → whole word only |
| \B | Non-word boundary | \Bword → "sword" not "word" |
Groups & Capturing
| Pattern | Description | Example |
|---|
| (abc) | Capturing group — captures "abc" | (\d+)-(\d+) → captures both |
| (?:abc) | Non-capturing group — groups without capturing | (?:https?):// |
| (?<name>abc) | Named capturing group | (?<year>\d{4})-(?<month>\d{2}) |
| \1 | Backreference to group 1 | (\w)\1 → aa, bb, cc |
| (a|b) | Alternation — matches a or b | (cat|dog) → cat or dog |
Lookahead & Lookbehind
| Pattern | Description | Example |
|---|
| (?=abc) | Positive lookahead — followed by "abc" | \d(?=px) → 5 in "5px" |
| (?!abc) | Negative lookahead — not followed by "abc" | \d(?!px) → 5 in "5em" |
| (?<=abc) | Positive lookbehind — preceded by "abc" | (?<=\$)\d+ → 99 in "$99" |
| (?<!abc) | Negative lookbehind — not preceded by "abc" | (?<!\$)\d+ → 99 in "£99" |
Flags
| Pattern | Description | Example |
|---|
| g | Global — find all matches, not just the first | /a/g finds every "a" |
| i | Case-insensitive matching | /hello/i → Hello, HELLO |
| m | Multiline — ^ and $ match line boundaries | /^line/m per line |
| s | Dotall — . matches newline characters | /.+/s spans lines |
| u | Unicode — enables Unicode property escapes | /\p{L}/u → letters |
| v | Unicode sets — set operations in character classes | /[\p{L}--[a-z]]/v |
Escape Sequences
| Pattern | Description | Example |
|---|
| \\ | Literal backslash | C:\\ → C:\ |
| \n | Newline | line1\nline2 |
| \t | Tab | col1\tcol2 |
| \/ | Literal forward slash (in /regex/ delimiters) | https:\/\/ |
| \p{L} | Unicode Letter property (requires u flag) | \p{L}+ → any script |
| \p{N} | Unicode Number property (requires u flag) | \p{N}+ → any numeral |
Test your patterns
Paste a regex pattern and test text to see highlighted matches and capture groups in real time.
Open Regex Tester