Cheat sheet
JavaScript regex cheat sheet
The whole syntax on one page, as of ECMAScript 2025. Tap any example to open it, with sample text, in the tester.
Nothing matches that filter.
Characters and escapes
Most characters match themselves. These have special meanings or stand for characters that are hard to type.
| Syntax | Meaning | Try it |
|---|---|---|
| . | Any character except line breaks (with the s flag: any character at all). | /c.t/g |
| \d \D | A digit 0–9 / anything that is not a digit. | /\d+/g |
| \w \W | A word character A–Z, a–z, 0–9 or _ / anything else. | /\w+/g |
| \s \S | Whitespace (spaces, tabs, line breaks, Unicode spaces) / anything else. | /\s+/g |
| \t \n \r | Tab, line feed and carriage return. | /\r?\n/g |
| \xhh \uhhhh | A character by its hex code: \x41 is A, \u00e9 is é. | /caf\u00e9/ |
| \u{hhhhh} | Any code point, including emoji. Needs the u or v flag. | /\u{1F600}/u |
| \. \* \\ | A backslash makes a special character literal: \. is a dot, \\ a backslash. | /\d+\.\d+/g |
Character classes
Square brackets match one character from a set.
| Syntax | Meaning | Try it |
|---|---|---|
| [abc] | One of a, b or c. | /gr[ae]y/g |
| [^abc] | Any one character except a, b and c. | /[^aeiou\s]+/g |
| [a-z0-9] | A range: any character from a to z, or 0 to 9. | /[A-F0-9]{2}/gi |
| [\s\S] | Any character including line breaks (the classic idiom before the s flag). | /<b>[\s\S]*?</b>/g |
| \p{L} \P{L} | A character with (or without) a Unicode property, here “letter”. Needs u or v. | /\p{L}+/gu |
| \p{Script=Greek} | A character from one script (also Latin, Cyrillic, Han, Arabic …). | /\p{Script=Greek}+/gu |
Anchors and boundaries
These match a position, not a character.
| Syntax | Meaning | Try it |
|---|---|---|
| ^ | Start of the text (with m: start of each line). | /^\w+/gm |
| $ | End of the text (with m: end of each line). | /\d+$/gm |
| \b | Word boundary: between a word character and a non-word character. | /\bcat\b/g |
| \B | Not a word boundary. | /\Bcat\B/g |
Quantifiers
A quantifier repeats the item just before it. They are greedy (take as much as possible) unless followed by ?.
| Syntax | Meaning | Try it |
|---|---|---|
| * | Zero or more times. | /go*gle/g |
| + | One or more times. | /go+gle/g |
| ? | Optional: zero or one time. | /colou?r/g |
| {3} {2,} {2,4} | Exactly 3, at least 2, between 2 and 4 times. | /\d{2,4}/g |
| *? +? ?? {2,4}? | Lazy versions: as few repetitions as possible. | /<.+?>/g |
Groups, alternation and backreferences
Parentheses group items, capture what they matched and limit the scope of |.
| Syntax | Meaning | Try it |
|---|---|---|
| a|b | Either a or b; alternatives are tried left to right. | /cat|dog/g |
| (…) | Capturing group: numbered from 1 by its opening parenthesis. | /(\d{4})-(\d{2})/g |
| (?<name>…) | Named group, available as match.groups.name. | /(?<user>\w+)@(?<host>[\w.]+)/g |
| (?:…) | Non-capturing group: groups without saving. | /(?:ab)+/g |
| \1 \k<name> | Backreference: the same text a group matched earlier. | /(['"]).*?\1/g |
| (?i:…) (?-i:…) | Modifiers: turn i, m or s on or off for part of the pattern (ES2025). | /(?i:hello) World/g |
Lookahead and lookbehind
Check what comes before or after a position without including it in the match.
| Syntax | Meaning | Try it |
|---|---|---|
| (?=…) | Positive lookahead: followed by … | /\d+(?=px)/g |
| (?!…) | Negative lookahead: not followed by … | /\d+(?![\dp])/g |
| (?<=…) | Positive lookbehind: preceded by … | /(?<=\$)\d+(?:\.\d\d)?/g |
| (?<!…) | Negative lookbehind: not preceded by … | /(?<!\$)\b\d+\b/g |
Flags
Written after the closing slash (/…/gi) or as the second argument of new RegExp.
| Syntax | Meaning | Try it |
|---|---|---|
| g | Global: find all matches, not just the first. | /o/g |
| i | Ignore case. | /regex/gi |
| m | Multiline: ^ and $ work per line. | /^#.*$/gm |
| s | dotAll: . also matches line breaks. | /start.*end/s |
| u | Unicode: code points, \u{…}, \p{…} and strict syntax. | /^.$/u |
| v | Unicode sets: u plus set operations and string properties. | /[\p{L}--[a-z]]+/gv |
| y | Sticky: match only at lastIndex, without searching ahead. | /\d/gy |
| d | Indices: record start and end positions of every group (match.indices). | /(\w+)@/d |
Unicode sets (v flag)
The v flag adds set operations inside classes and properties that match whole emoji sequences.
| Syntax | Meaning | Try it |
|---|---|---|
| [A--B] | Subtraction: in A but not in B. | /[\p{L}--\p{ASCII}]+/gv |
| [A&&B] | Intersection: in both A and B. | /[\p{L}&&\p{ASCII}]+/gv |
| [[a-z][0-9]] | Nested classes (union). | /[[a-f][0-9]]+/gv |
| \q{abc|def} | String literals inside a class. | /[\q{Mr|Mrs|Ms}]\./gv |
| \p{RGI_Emoji} | A whole emoji, including flags and joined sequences. | /\p{RGI_Emoji}/gv |
Replacement patterns
Special sequences in the replacement string of replace() and replaceAll().
| Syntax | Meaning | Try it |
|---|---|---|
| $1 $2 … | The text of group 1, 2 … | /(\w+) (\w+)/ |
| $<name> | The text of a named group. | /(?<d>\d\d)\/(?<m>\d\d)\/(?<y>\d{4})/g |
| $& | The whole match. | /\d+/g |
| $` $' | The text before / after the match. | /=/ |
| $$ | A literal dollar sign. | /(\d+)/g |
JavaScript methods
Where a regex is used in JavaScript, and what each method returns.
| Syntax | Meaning | Try it |
|---|---|---|
| re.test(str) | true or false. With g or y it starts from (and moves) re.lastIndex. | |
| re.exec(str) | The next match with groups and index, or null. | |
| str.match(re) | Without g: like exec. With g: an array of all matched texts. | |
| str.matchAll(re) | An iterator over every match with its groups (re must have g). | |
| str.replace(re, x) | Replaces the first match, or all of them with g; x may be a function. | |
| str.replaceAll(re, x) | Replaces every match (re must have g). | |
| str.split(re) | Splits at each match; captured groups are included in the result. | |
| str.search(re) | Index of the first match, or −1. |
Notes on JavaScript’s flavour
JavaScript regexes are close to Perl and PCRE but not identical. There are no possessive quantifiers or atomic groups, no \A, \Z or \z (use ^ and $ without the m flag), and $ never matches before a final newline unless m is on. On the other hand, lookbehind can contain any pattern, even variable-length ones.
\d, \w and \b only know ASCII, even with the u flag. For other scripts use Unicode property escapes such as \p{L} (letters) or \p{Nd} (decimal digits), which need u or v.
When you build a regex from a string, new RegExp("\\d+"), every backslash must be doubled, because the string literal consumes one level of escaping. The regex literal /\d+/ does not have that problem.
Questions
What is the difference between u and v?
v (ES2024) includes everything u does and adds set operations in classes, nested classes, \q{…} strings and properties of strings such as \p{RGI_Emoji}. A few characters must be escaped inside classes in v mode, and you cannot use both flags at once.
Which browsers support lookbehind and named groups?
All current browsers. Safari was the last to add lookbehind, in version 16.4 (2023).
Why does test() give different answers on repeated calls?
With the g or y flag, test() and exec() start from lastIndex and move it after each match. Reset re.lastIndex = 0, or drop the g flag when you only need a yes or no.