Regex Cheat Sheet
Look up any JavaScript regex construct — syntax, meaning and a worked example whose result this page computes, with the tester right above the table.
Edit the pattern, flags or test string here. Every “Try it” button below loads that row's example into this tester.
Drop a text file on the test string pane — it is read on this device and never uploaded.
Native JavaScript replace semantics — the preview pane below updates as you type.
Press ? for shortcuts
All 73 constructs shown
Character classes
Character classes — syntax, meaning, example and computed result
| Syntax | Meaning | Example | Result |
|---|---|---|---|
. | Matches any single character except a line terminator. With the s flag it matches line terminators too; inside a character class it is an ordinary dot and needs no backslash. | a.c | /a.c/ "abc" → ✓ "abc" "a\nc" → ✗ no match |
\d | Matches one ASCII digit, 0 to 9. It never matches digits from other scripts, even with the u flag. | \d | /\d/ "a1" → ✓ "1" "abc" → ✗ no match |
\D | Matches one character that is not an ASCII digit. | \D | /\D/ "12a" → ✓ "a" "123" → ✗ no match |
\w | Matches one ASCII letter, digit or underscore — the same set as [A-Za-z0-9_]. Accented letters such as é are not word characters; use \p{L} with the u flag for those. | \w | /\w/ "_a" → ✓ "_" "!?" → ✗ no match |
\W | Matches one character outside [A-Za-z0-9_]. | \W | /\W/ "a!" → ✓ "!" "abc" → ✗ no match |
\s | Matches one whitespace character: space, tab, form feed, vertical tab, a line terminator or a Unicode space separator. | \s | /\s/ "a b" → ✓ " " "ab" → ✗ no match |
\S | Matches one character that is not whitespace. | \S | /\S/ " a" → ✓ "a" " " → ✗ no match |
[abc] | Matches any one of the characters listed between the brackets. | [abc] | /[abc]/ "cat" → ✓ "c" "dog" → ✗ no match |
[^abc] | Matches any one character that is not listed after the caret. It still has to consume a character, so it never matches at the end of the input. | [^abc] | /[^abc]/ "dog" → ✓ "d" "cab" → ✗ no match |
[a-z] | Matches one character whose code point falls between the two characters around the hyphen. | [a-z] | /[a-z]/ "Zebra" → ✓ "e" "ZEBRA" → ✗ no match |
[a-] | A hyphen first or last inside a class is a literal hyphen, not a range. | [a-] | /[a-]/ "-" → ✓ "-" "b" → ✗ no match |
[\]] | An escaped closing bracket inside a class matches a literal ]. Without the backslash the class would end at that bracket. | [\]] | /[\]]/ "]" → ✓ "]" "[" → ✗ no match |
[a^] | A caret anywhere but the first position inside a class is a literal caret. | [a^] | /[a^]/ "^" → ✓ "^" "b" → ✗ no match |
[\b] | Inside a character class, \b matches the backspace character U+0008. This is the one place where \b is not a word boundary. | [\b] | /[\b]/ "\b" → ✓ "\b" "b" → ✗ no match |
Anchors
Anchors — syntax, meaning, example and computed result
| Syntax | Meaning | Example | Result |
|---|---|---|---|
^ | Asserts the position at the start of the input. With the m flag it also asserts the start of every line. | ^cat | /^cat/ "cat nap" → ✓ "cat" "a cat" → ✗ no match |
$ | Asserts the position at the end of the input. With the m flag it also asserts the end of every line. | cat$ | /cat$/ "a cat" → ✓ "cat" "cat nap" → ✗ no match |
\b | Asserts a position where a word character meets a non-word character or the edge of the input. It matches a position, not a character, so it consumes nothing. | \bcat\b | /\bcat\b/ "cat nap" → ✓ "cat" "concat" → ✗ no match |
\B | Asserts a position that is not a word boundary: either both sides are word characters, or neither is. | \Bcat | /\Bcat/ "concat" → ✓ "cat" "cat nap" → ✗ no match |
Quantifiers
Quantifiers — syntax, meaning, example and computed result
| Syntax | Meaning | Example | Result |
|---|---|---|---|
* | Repeats the preceding item zero or more times, taking as many as it can. A quantifier applies to the single item before it, so ab* repeats b, not ab. | ab*c | /ab*c/ "ac" → ✓ "ac" "abbbc" → ✓ "abbbc" |
+ | Repeats the preceding item one or more times, taking as many as it can. | ab+c | /ab+c/ "abbc" → ✓ "abbc" "ac" → ✗ no match |
? | Makes the preceding item optional: zero or one occurrence. | colou?r | /colou?r/ "color" → ✓ "color" "colour" → ✓ "colour" |
{3} | Repeats the preceding item exactly that many times. | \d{3} | /\d{3}/
"12345" → ✓ "123"
"12" → ✗ no match |
{2,} | Repeats the preceding item at least that many times, taking as many as it can. | \d{2,} | /\d{2,}/
"1234" → ✓ "1234"
"1" → ✗ no match |
{2,5} | Repeats the preceding item between the two counts, preferring the longer run. The counts are a preference, not a boundary: without an anchor the match can start anywhere. | \d{2,5} | /\d{2,5}/
"1234567" → ✓ "12345"
"1" → ✗ no match |
*? | Repeats zero or more times, preferring the shortest run that still lets the pattern match. | <.*?> | /<.*?>/ "<a><b>" → ✓ "<a>" |
+? | Repeats one or more times, preferring the shortest run. | \d+? | /\d+?/ "1234" → ✓ "1" |
?? | Matches zero or one occurrence, preferring zero. | ab?? | /ab??/ "ab" → ✓ "a" |
{2,5}? | Repeats between the two counts, preferring the fewest. | \d{2,5}? | /\d{2,5}?/
"1234567" → ✓ "12" |
{ | A brace that does not open a valid quantifier is matched as a literal { character. The u and v flags reject it instead: under those flags a lone brace is a syntax error. | a{ | /a{/
"a{" → ✓ "a{"
"a" → ✗ no match |
Groups and references
Groups and references — syntax, meaning, example and computed result
| Syntax | Meaning | Example | Result |
|---|---|---|---|
(…) | Groups the enclosed pattern and captures what it matched for later reference. | (ab)+ | /(ab)+/ "abab" → ✓ "abab" "ba" → ✗ no match |
(?:…) | Groups the enclosed pattern without capturing it. | (?:ab)+ | /(?:ab)+/ "abab" → ✓ "abab" "ba" → ✗ no match |
(?<name>…)Added in ES2018 | Captures the enclosed pattern under a name, readable from the match's groups object. A name may be reused only by groups in different alternation branches (ES2025); otherwise it is a syntax error. | (?<year>\d{4}) | /(?<year>\d{4})/
"in 2026" → ✓ "2026"
"in 26" → ✗ no match |
\1 | Matches the same text an earlier capture group matched, counted from the left. | (\w)\1 | /(\w)\1/ "book" → ✓ "oo" "abc" → ✗ no match |
\k<name>Added in ES2018 | Matches the same text the named group matched. | (?<c>\w)\k<c> | /(?<c>\w)\k<c>/ "letter" → ✓ "tt" "abc" → ✗ no match |
| | Tries the left side, then the right — the lowest-precedence operator in a pattern. It splits everything around it unless a group encloses it. | cat|dog | /cat|dog/ "hotdog" → ✓ "dog" "bird" → ✗ no match |
Lookaround
Lookaround — syntax, meaning, example and computed result
| Syntax | Meaning | Example | Result |
|---|---|---|---|
(?=…) | Asserts that what follows matches, without consuming it. | \bcat(?= nap) | /\bcat(?= nap)/ "cat nap" → ✓ "cat" "cat food" → ✗ no match |
(?!…) | Asserts that what follows does not match, without consuming it. | \bcat(?! nap) | /\bcat(?! nap)/ "cat food" → ✓ "cat" "cat nap" → ✗ no match |
(?<=…)Added in ES2018 | Asserts that what precedes the current position matches. JavaScript lookbehind may be variable length, unlike PCRE's fixed-length rule. | (?<=€)\d+ | /(?<=€)\d+/ "€42" → ✓ "42" "$42" → ✗ no match |
(?<!…)Added in ES2018 | Asserts that what precedes the current position does not match. | (?<!€)\d+ | /(?<!€)\d+/ "$42" → ✓ "42" "€4" → ✗ no match |
Escapes
Escapes — syntax, meaning, example and computed result
| Syntax | Meaning | Example | Result |
|---|---|---|---|
\. | A backslash makes the next metacharacter literal — here, a dot that matches only a dot. | 3\.14 | /3\.14/ "3.14" → ✓ "3.14" "3x14" → ✗ no match |
\t | Matches the tab character U+0009. | \t | /\t/ "a\tb" → ✓ "\t" "a b" → ✗ no match |
\n | Matches the line feed character U+000A. | a\nb | /a\nb/ "a\nb" → ✓ "a\nb" "a b" → ✗ no match |
\r | Matches the carriage return character U+000D. | \r | /\r/ "a\r\n" → ✓ "\r" "a\n" → ✗ no match |
\f | Matches the form feed character U+000C. | \f | /\f/ "a\fb" → ✓ "\f" "a b" → ✗ no match |
\v | Matches the vertical tab character U+000B. As a flag letter, v means something else entirely: Unicode sets. | \v | /\v/ "a\u000bb" → ✓ "\u000b" "a b" → ✗ no match |
\0 | Matches the null character U+0000. It is the null character only when no digit follows it. | \0 |