You are encouraged to read the regular expressions guide to get an overview of the available regex syntaxes and how they work.
Regular expressions are a important concept in formal language theory. They are a way to describe a possibly infinite set of character strings (called a language). A regular expression, at its core, needs the following features:
- A set of characters that can be used in the language, called the alphabet.
abmeans "the character
afollowed by the character
- Kleene star:
a*means "zero or more
Creating regular expressions
A regular expression is typically created as a literal by enclosing a pattern in forward slashes (
const regex1 = /ab+c/g;
Regular expressions can also be created with the
const regex2 = new RegExp("ab+c", "g");
They have no runtime differences, although they may have implications on performance, static analyzability, and authoring ergonomic issues with escaping characters. For more information, see the
Flags are special parameters that can change the way a regular expression is interpreted or the way it interacts with the input text. Each flag corresponds to one accessor property on the
||Generate indices for substring matches.||
||"Unicode"; treat a pattern as a sequence of Unicode code points.||
||Perform a "sticky" search that matches starting at the current position in the target string.||
The sections below list all available regex syntaxes, grouped by their syntactic nature.
Assertions are constructs that test whether the string meets a certain condition at the specified position, but not consume characters. Assertions cannot be quantified.
- Input boundary assertion:
Asserts that the current position is the start or end of input, or start or end of a line if the
mflag is set.
- Lookahead assertion:
Asserts that the current position is followed or not followed by a certain pattern.
- Lookbehind assertion:
Asserts that the current position is preceded or not preceded by a certain pattern.
- Word boundary assertion:
Asserts that the current position is a word boundary.
Atoms are the most basic units of a regular expression. Each atom consumes one or more characters in the string, and either fails the match or allows the pattern to continue matching with the next atom.
Matches a previously matched subpattern captured with a capturing group.
- Capturing group:
Matches a subpattern and remembers information about the match.
- Character class:
Matches any character in or not in a set of characters.
- Character class escape:
Matches any character in or not in a predefined set of characters.
- Character escape:
Matches a character that may not be able to be conveniently represented in its literal form.
- Literal character:
Matches a specific character.
- Named backreference:
Matches a previously matched subpattern captured with a named capturing group.
- Named capturing group:
Matches a subpattern and remembers information about the match. The group can later be identified by a custom name instead of by its index in the pattern.
- Non-capturing group:
Matches a subpattern without remembering information about the match.
- Unicode character class escape:
Matches any character in or not in a set of Unicode characters identified by a Unicode property.
Matches any character except line terminators, unless the
sflag is set.
These features do not specify any pattern themselves, but are used to compose patterns.
Matches any of a set of alternatives separated by the
Matches an atom a certain number of times.
BCD tables only load in the browser