Regular expressions
Regular expressions describe text patterns for searching, validation, and extraction. Modern JavaScript supports named groups, lookbehind, and Unicode-aware matching features.
Overview
Regular expressions describe text patterns for searching, validation, and extraction. Modern JavaScript supports named groups, lookbehind, and Unicode-aware matching features.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 1 | 12 | 1 | 1 | 18 | 1 | |
| The [Symbol.match]() method of RegExp instances specifies how String.prototype.match() should behave. In addition, its presence (or absence) can influence whether an object is regarded as a regular expression. | 50 | 13 | 49 | 10 | 50 | 10 |
| The [Symbol.matchAll]() method of RegExp instances specifies how String.prototype.matchAll should behave. | 73 | 79 | 67 | 13 | 73 | 13 |
| The [Symbol.replace]() method of RegExp instances specifies how String.prototype.replace() and String.prototype.replaceAll() should behave when the regular expression is passed in as the pattern. | 50 | 79 | 49 | 10 | 50 | 10 |
| The [Symbol.search]() method of RegExp instances specifies how String.prototype.search should behave. | 50 | 13 | 49 | 10 | 50 | 10 |
| The RegExp[Symbol.species] static accessor property returns the constructor used to construct copied regular expressions in certain RegExp methods. | 51 | 13 | 49 | 10 | 51 | 10 |
| The [Symbol.split]() method of RegExp instances specifies how String.prototype.split should behave when the regular expression is passed in as the separator. | 50 | 79 | 49 | 10 | 50 | 10 |
| The RegExp() constructor creates RegExp objects. | 1 | 12 | 1 | 1 | 18 | 1 |
| The dotAll accessor property of RegExp instances returns whether or not the s flag is used with this regular expression. | 62 | 79 | 78 | 11.1 | 62 | 11.3 |
| The exec() method of RegExp instances executes a search with this regular expression for a match in a specified string and returns a result array, or null. | 1 | 12 | 1 | 1 | 18 | 1 |
| The flags accessor property of RegExp instances returns the flags of this regular expression. | 49 | 79 | 37 | 9 | 49 | 9 |
| The global accessor property of RegExp instances returns whether or not the g flag is used with this regular expression. | 1 | 12 | 1 | 1 | 18 | 1 |
RegExp.global.prototype accessor Prototype accessor property (ES2015) | 48 | 12 | 38 | 1.3 | 48 | 1 |
| The hasIndices accessor property of RegExp instances returns whether or not the d flag is used with this regular expression. | 90 | 90 | 88 | 15 | 90 | 15 |
| The ignoreCase accessor property of RegExp instances returns whether or not the i flag is used with this regular expression. | 1 | 12 | 1 | 1 | 18 | 1 |
RegExp.ignoreCase.prototype accessor Prototype accessor property (ES2015) | 48 | 12 | 38 | 1.3 | 48 | 1 |
| The lastIndex data property of a RegExp instance specifies the index at which to start the next match. | 1 | 12 | 1 | 1 | 18 | 1 |
| The multiline accessor property of RegExp instances returns whether or not the m flag is used with this regular expression. | 1 | 12 | 1 | 1 | 18 | 1 |
RegExp.multiline.prototype accessor Prototype accessor property (ES2015) | 48 | 12 | 38 | 1.3 | 48 | 1 |
| The source accessor property of RegExp instances returns a string containing the source text of this regular expression, without the two forward slashes on both sides or any flags. | 1 | 12 | 1 | 1 | 18 | 1 |
RegExp.source.empty regex string "(?:)" for empty regexps | 6 | 12 | 38 | 5 | 18 | 4.2 |
RegExp.source.escaping Line breaks and slashes are escaped | 73 | 12 | 38 | 6 | 73 | 6 |
RegExp.source.prototype accessor Prototype accessor property (ES2015) | 48 | 12 | 41 | 1.3 | 48 | 1 |
| The sticky accessor property of RegExp instances returns whether or not the y flag is used with this regular expression. | 49 | 13 | 3 | 10 | 49 | 10 |
RegExp.sticky.anchored sticky flag Anchored sticky flag behavior per ES2015 | 49 | 13 | 44 | 10 | 49 | 10 |
RegExp.sticky.prototype accessor Prototype accessor property (ES2015) | 49 | 13 | 38 | 10 | 49 | 10 |
| The test() method of RegExp instances executes a search with this regular expression for a match between a regular expression and a specified string. Returns true if there is a match; false otherwise. | 1 | 12 | 1 | 1 | 18 | 1 |
| The toString() method of RegExp instances returns a string representing this regular expression. | 1 | 12 | 1 | 1 | 18 | 1 |
RegExp.toString.escaping Line breaks and slashes are escaped | 73 | 12 | 38 | 6 | 73 | 6 |
| The unicode accessor property of RegExp instances returns whether or not the u flag is used with this regular expression. | 50 | 12 | 46 | 10 | 50 | 10 |
| The unicodeSets accessor property of RegExp instances returns whether or not the v flag is used with this regular expression. | 112 | 112 | 116 | 17 | 112 | 17 |
| Other | ||||||
| Regular expression literals are enclosed by two forward slashes (/). The lexer consumes all characters up to the next unescaped forward slash or the end of the line, unless the forward slash appears within a character class ([]). Some characters (namely, those that are identifier parts) can appear after the closing slash, denoting flags. | 1 | 12 | 1 | 1 | 18 | 1 |
| A backreference refers to the submatch of a previous capturing group and matches the same text as that group. For named capturing groups, you may prefer to use the named backreference syntax. | 1 | 12 | 1 | 1 | 18 | 1 |
| A capturing group groups a subpattern, allowing you to apply a quantifier to the entire group or use disjunctions within it. It memorizes information about the subpattern match, so that you can refer back to it later with a backreference, or access the information through the match results. | 1 | 12 | 1 | 1 | 18 | 1 |
| A character class matches any character in or not in a custom set of characters. When the v flag is enabled, it can also be used to match finite-length strings. | 1 | 12 | 1 | 1 | 18 | 1 |
| A character class escape is an escape sequence that represents a set of characters. | 1 | 12 | 1 | 1 | 18 | 1 |
| A character escape represents a character that may not be able to be conveniently represented in its literal form. | 1 | 12 | 1 | 1 | 18 | 1 |
javascript.regular_expressions.character_escape.unicode Unicode character escape: `\u{...}` | 50 | 12 | 46 | 10 | 50 | 10 |
| A disjunction specifies multiple alternatives. Any alternative matching the input causes the entire disjunction to be matched. | 1 | 12 | 1 | 1 | 18 | 1 |
| An input boundary assertion checks if the current position in the string is an input boundary. An input boundary is the start or end of the string; or, if the m flag is set, the start or end of a line. | 1 | 12 | 1 | 1 | 18 | 1 |
| A literal character specifies exactly itself to be matched in the input text. | 1 | 12 | 1 | 1 | 18 | 1 |
| A lookahead assertion "looks ahead": it attempts to match the subsequent input with the given pattern, but it does not consume any of the input — if the match is successful, the current position in the input stays the same. | 1 | 12 | 1 | 1 | 18 | 1 |
| A lookbehind assertion "looks behind": it attempts to match the previous input with the given pattern, but it does not consume any of the input — if the match is successful, the current position in the input stays the same. It matches each atom in its pattern in the reverse order. | 62 | 79 | 78 | 16.4 | 62 | 16.4 |
| A modifier overrides flag settings in a specific part of a regular expression. It can be used to enable or disable flags that change the meanings of certain regex syntax elements. These flags are i, m, and s. | 125 | 125 | 132 | 26 | 125 | 26 |
| A named backreference refers to the submatch of a previous named capturing group and matches the same text as that group. For unnamed capturing groups, you need to use the normal backreference syntax. | 64 | 79 | 78 | 11.1 | 64 | 11.3 |
| A named capturing group is a particular kind of capturing group that allows to give a name to the group. The group's matching result can later be identified by this name instead of by its index in the pattern. | 64 | 79 | 78 | 11.1 | 64 | 11.3 |
javascript.regular_expressions.named_capturing_group.duplicate_named_capturing_groups Duplicate names in different disjunction alternatives are allowed | 125 | 125 | 129 | 17 | 125 | 17 |
| A non-capturing group groups a subpattern, allowing you to apply a quantifier to the entire group or use disjunctions within it. It acts like the grouping operator in JavaScript expressions, and unlike capturing groups, it does not memorize the matched text, allowing for better performance and avoiding confusion when the pattern also contains useful… | 1 | 12 | 1 | 1 | 18 | 1 |
| A quantifier repeats an atom a certain number of times. The quantifier is placed after the atom it applies to. | 1 | 12 | 1 | 1 | 18 | 1 |
| A unicode character class escape is a kind of character class escape that matches a set of characters specified by a Unicode property. It's only supported in Unicode-aware mode. When the v flag is enabled, it can also be used to match finite-length strings. | 64 | 79 | 78 | 11.1 | 64 | 11.3 |
| A wildcard matches all characters except line terminators. It also matches line terminators if the s flag is set. | 1 | 12 | 1 | 1 | 18 | 1 |
| A word boundary assertion checks if the current position in the string is a word boundary. A word boundary is where the next character is a word character and the previous character is not a word character, or vice versa. | 1 | 12 | 1 | 1 | 18 | 1 |
- Case folding is implemented in version 13
Syntax
// Named capture groups
const dateRegex = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
const match = '2026-04-06'.match(dateRegex);
console.log(match.groups.year); // '2026'
console.log(match.groups.month); // '04'
// dotAll flag (s)
/hello.world/s.test('hello\
world'); // true
// Unicode properties
/\p{Script=Han}/u.test('漢'); // true</day></month></year> Live demo
Multiple rowmatch with DotAll furag.
S furag specified and, dt(.) that change rowco-d to also matchlike to..
Unicode Propertyescape-p
U furag and \p{...} Use, specific. Characterbody type(character etc) distinguish..
Use cases
Validation and parsing
Check formats such as IDs, tags, or structured tokens and extract the parts you need.
Search and replacement
Find repeated patterns across text for cleanup, highlighting, or tokenization.
Cautions
- Unreadable expressions are hard to maintain, so use comments, named groups, or simpler parsing when the pattern gets too complex.
- A regex that matches syntax does not guarantee semantic validity, so do not over-trust pattern checks alone.
Accessibility
- When regex-based validation fails, explain the required format in plain language instead of exposing only a pattern-like error.
Related links
Powered by web-features