Regex Cheat Sheet
A complete JavaScript regular expression reference. All syntax elements with descriptions and examples — bookmark this page for quick lookups while writing regex patterns.
Open the Regex TesterCharacter Classes
Character classes match a single character from a defined set.
| Syntax | Description |
|---|---|
| . | Any character except newline (use s flag to include newlines) |
| \d | Any digit — equivalent to [0-9] |
| \D | Any non-digit character |
| \w | Word character — [a-zA-Z0-9_] |
| \W | Non-word character |
| \s | Any whitespace — space, tab, newline |
| \S | Any non-whitespace character |
| [abc] | Character set — matches a, b, or c |
| [^abc] | Negated set — matches anything except a, b, or c |
| [a-z] | Character range — matches any letter a through z |
Quantifiers
Quantifiers specify how many times the preceding element must occur. By default they are greedy (match as many characters as possible). Add ? to make them lazy.
| Syntax | Description |
|---|---|
| * | 0 or more (greedy) |
| + | 1 or more (greedy) |
| ? | 0 or 1 — makes the preceding element optional |
| {n} | Exactly n times |
| {n,} | n or more times |
| {n,m} | Between n and m times (inclusive) |
| *? | 0 or more (lazy — matches as few as possible) |
| +? | 1 or more (lazy) |
| ?? | 0 or 1 (lazy) |
Anchors
Anchors match a position in the string rather than a character.
| Syntax | Description |
|---|---|
| ^ | Start of string. With m flag: start of each line |
| $ | End of string. With m flag: end of each line |
| \b | Word boundary — position between a word char and non-word char |
| \B | Non-word boundary |
Groups & Alternation
Groups capture portions of the match for extraction or backreference. Use them to apply quantifiers to multi-character sequences.
| Syntax | Description |
|---|---|
| (abc) | Capture group — captured in match[1], match[2], etc. |
| (?:abc) | Non-capturing group — groups without capturing |
| (?<name>abc) | Named capture group — accessible as match.groups.name |
| \1, \2 | Backreference to capture group 1, 2… (in pattern) |
| $1, $2 | Backreference in replace string (use $& for full match) |
| a|b | Alternation — matches a or b |
Lookahead & Lookbehind
Lookaround assertions match a position based on what precedes or follows, without consuming characters.
| Syntax | Description |
|---|---|
| (?=abc) | Positive lookahead — matches if followed by abc |
| (?!abc) | Negative lookahead — matches if NOT followed by abc |
| (?<=abc) | Positive lookbehind — matches if preceded by abc |
| (?<!abc) | Negative lookbehind — matches if NOT preceded by abc |
Flags
Flags modify how the regex engine interprets the pattern. In JavaScript, flags are passed as the second argument to new RegExp() or appended after the closing / delimiter.
| Syntax | Description |
|---|---|
| g | Global — find all matches, not just the first |
| i | Case-insensitive — treat uppercase and lowercase as equal |
| m | Multiline — ^ and $ match start/end of each line |
| s | DotAll — makes . match newlines too |
| u | Unicode — full Unicode support, enables \p{} escapes |
| y | Sticky — matches only at lastIndex position |
| d | Indices — includes start/end indices of each match group |
Special Characters & Escaping
These characters have special meaning in regex. Prefix them with \ to match them literally.
| Syntax | Description |
|---|---|
| \. | Literal dot (unescaped . matches any char) |
| \* | Literal asterisk |
| \n | Newline |
| \t | Tab character |
| \r | Carriage return |
| \uXXXX | Unicode code point (4 hex digits) |
Ready to test a pattern? Open the live regex tester — real-time matching, group highlighting, and a patterns library.
Open Regex Tester