Regex Cheat Sheet
JavaScript regex syntax, token by token, searchable by category.
This is JavaScript regex, the engine this site's tools run. Not PCRE, Python or POSIX. JavaScript has no possessive quantifiers, atomic groups, recursion or verbose flag.
A pattern you write here behaves the same in the Regex Tester, the regex drill and a Sigma rule's |re modifier.
70 rows.
| Token | Meaning |
|---|---|
| ^ | Matches the start of the string, or the start of a line when the m (multiline) flag is set. |
| $ | Matches the end of the string, or the end of a line when the m flag is set. |
| \b | Zero-width word boundary: matches the position between a \w character and a non-word character (or a string edge). |
| \B | Zero-width non-boundary: matches a position where \b would NOT match (between two word characters, or two non-word characters). |
| . | Matches any single character except line terminators, unless the s (dotAll) flag is set. |
| \d | Matches a single decimal digit; equivalent to [0-9]. |
| \D | Matches any character that is not a decimal digit; equivalent to [^0-9]. |
| \w | Matches a "word" character: any of [A-Za-z0-9_]. ASCII only, it does not match accented or non-Latin letters. |
| \W | Matches any character that is not a word character; equivalent to [^A-Za-z0-9_]. |
| \s | Matches a whitespace character: JS's built-in \s is broader than just space/tab/newline; it also covers several Unicode space separators and line terminators. |
| \S | Matches any non-whitespace character. |
| [...] | Character class: matches any one character listed inside the brackets; most metacharacters lose their special meaning inside a class. |
| [^...] | Negated character class: matches any one character NOT listed inside the brackets. |
| [a-z] | A range inside a character class: matches any single character between the two endpoints, inclusive. |
| \p{...} | Unicode property escape: matches any character with the given Unicode property or script; requires the u or v flag, otherwise \p is just a literal "p". |
| \P{...} | Negated Unicode property escape: matches any character that does NOT have the given property; also requires u or v. |
| * | Matches the preceding element zero or more times, greedily. |
| + | Matches the preceding element one or more times, greedily. |
| ? | Matches the preceding element zero or one times (makes it optional). |
| {n} | Matches the preceding element exactly n times. |
| {n,} | Matches the preceding element n or more times. |
| {n,m} | Matches the preceding element between n and m times, inclusive. |
| *? | Lazy version of *: matches as few repetitions as possible, only backtracking to take more if the rest of the pattern requires it. |
| +? | Lazy version of +: matches the fewest repetitions (at least one) needed for the overall match to succeed. |
| ?? | Lazy version of ?: prefers to match zero occurrences, only taking one if required. |
| {n,m}? | Lazy version of {n,m}: matches as close to the minimum n as possible. |
| (...) | Capturing group: groups a sub-pattern and remembers the text it matched, numbered by the order of its opening parenthesis. |
| (?:...) | Non-capturing group: groups a sub-pattern (e.g. for alternation or a quantifier) without creating a numbered capture. |
| (?<name>...) | Named capturing group: same as a numbered group, but also accessible by name via the match's .groups object. |
| | | Alternation: matches whichever of the patterns on either side succeeds first. |
| \1, \2, ... | Numbered backreference: matches the same text that a previous numbered capturing group matched. |
| \k<name> | Named backreference: matches the same text a previous named capturing group matched. |
| (?:...)? | An optional non-capturing group: a common way to make a whole clause optional without capturing it. |
| (?=...) | Positive lookahead: asserts that the given pattern matches immediately after the current position, without consuming those characters. |
| (?!...) | Negative lookahead: asserts that the given pattern does NOT match immediately after the current position. |
| (?<=...) | Positive lookbehind: asserts that the given pattern matches immediately before the current position, without consuming those characters. A comparatively late addition to JS (ES2018); very old browser engines may lack it. |
| (?<!...) | Negative lookbehind: asserts that the given pattern does NOT match immediately before the current position. |
| g | Global: find all matches in the string instead of stopping after the first, and makes exec()/replaceAll() scan forward via lastIndex. |
| i | Case-insensitive: letters match regardless of case. |
| m | Multiline: makes ^ and $ additionally match right after/before line terminators, not just the string's absolute start/end. |
| s | dotAll: lets . also match line terminators, which it otherwise excludes. |
| u | Unicode: treats the pattern as a sequence of Unicode code points rather than raw UTF-16 code units, enables \u{...} and \p{...}/\P{...} escapes, and rejects several otherwise-lenient escape typos. Without it, a supplementary-plane character (like most emoji) can be split into its two surrogate halves by . or a character class. |
| v | unicodeSets: a strict superset of u (added in ES2024) that additionally allows set operations (union, intersection, subtraction) inside character classes; u and v cannot both be set on the same pattern. |
| y | Sticky: matches only starting exactly at lastIndex, without scanning forward to find the next possible match position. |
| d | hasIndices: makes the match result carry an additional .indices array (and .indices.groups for named groups) giving each match/group's [start, end] character offsets. |
| \\ | Escapes a following regex metacharacter so it is matched as a literal character. |
| \. | Matches a literal dot character (a bare . means "any character"). |
| \n | Matches a line feed (LF, U+000A) character. |
| \r | Matches a carriage return (CR, U+000D) character. |
| \t | Matches a horizontal tab character. |
| \0 | Matches the NUL (U+0000) character, as long as it isn't immediately followed by another digit. |
| \xHH | Matches the character with the given 2-digit hexadecimal code (the Latin-1 range, 00-FF). |
| \uHHHH | Matches the UTF-16 code unit with the given 4-digit hexadecimal code. |
| \u{HHHHH} | Matches the full Unicode code point with the given hex value (1-6 digits); requires the u or v flag. Correctly matches supplementary-plane characters as one unit instead of splitting their UTF-16 surrogate pair. |
| [\b] | Inside a character class only, \b means the backspace control character (U+0008), not a word boundary; \b only means "word boundary" outside a character class. |
| $1, $2, ... | In a String.prototype.replace()/replaceAll() replacement string, inserts the text captured by numbered group n. |
| $<name> | Inserts the text captured by a named group, in a replacement string. |
| $& | Inserts the entire matched substring. |
| $` | Inserts the portion of the input string that comes before the matched substring. |
| $' | Inserts the portion of the input string that comes after the matched substring. |
| $$ | Inserts a literal dollar-sign character in the output (escapes the special meaning of $). |
| function replacer | A function, instead of a string pattern, can be passed as the replacement: it's called once per match and its return value is substituted in. |
| .test(str) | RegExp method: returns true or false depending on whether the pattern matches anywhere in the string. |
| .exec(str) | RegExp method: returns the next match (as an array with capture groups) or null; with the g or y flag, it advances .lastIndex so a repeated call continues from where the last one left off. |
| String.prototype.match() | Without the g flag, returns one match array (with groups); with g, returns all matched substrings as a plain array with no group info. |
| String.prototype.matchAll() | Returns an iterator over every match, each a full match object (with .groups); requires the regex to have the g flag or it throws. |
| .replace() / .replaceAll() | Substitutes matches with a replacement string or function; replaceAll() specifically requires a global (g) regex, or it throws a TypeError. |
| String.prototype.search() | Returns the character index of the first match, or -1 if there isn't one (ignores any g flag). |
| String.prototype.split() | Splits a string into an array of substrings wherever the pattern matches. |
| .lastIndex | A property on a global (g) or sticky (y) RegExp object marking where the next exec()/test() call resumes from; forgetting to reset it to 0 before re-scanning a string from the start is a classic source of silently skipped matches. |