Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Regular expressions

Aregular expression (regex for short) allow developers to match strings against a pattern, extract submatch information, or simply test if the string conforms to that pattern. Regular expressions are used in many programming languages, and JavaScript's syntax is inspired byPerl.

You are encouraged to read theregular expressions guide to get an overview of the available regex syntaxes and how they work.

Description

Regular expressions are a important concept in formal language theory. They are a way to describe a possibly infinite set of character strings (called alanguage). A regular expression, at its core, needs the following features:

  • A set ofcharacters that can be used in the language, called thealphabet.
  • Concatenation:ab means "the charactera followed by the characterb".
  • Union:a|b means "eithera orb".
  • Kleene star:a* means "zero or morea characters".

Assuming a finite alphabet (such as the 26 letters of the English alphabet, or the entire Unicode character set), all regular languages can be generated by the features above. Of course, many patterns are very tedious to express this way (such as "10 digits" or "a character that's not a space"), so JavaScript regular expressions include many shorthands, introduced below.

Note:JavaScript regular expressions are in fact not regular, due to the existence ofbackreferences (regular expressions must have finite states). However, they are still a very useful feature.

Creating regular expressions

A regular expression is typically created as a literal by enclosing a pattern in forward slashes (/):

js
const regex1 = /ab+c/g;

Regular expressions can also be created with theRegExp() constructor:

js
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 theRegExp reference.

Regex flags

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 theRegExp object.

FlagDescriptionCorresponding property
dGenerate indices for substring matches.hasIndices
gGlobal search.global
iCase-insensitive search.ignoreCase
mMakes^ and$ match the start and end of each line instead of those of the entire string.multiline
sAllows. to match newline characters.dotAll
u"Unicode"; treat a pattern as a sequence of Unicode code points.unicode
vAn upgrade to theu mode with more Unicode features.unicodeSets
yPerform a "sticky" search that matches starting at the current position in the target string.sticky

Thei,m, ands flags can be enabled or disabled for specific parts of a regex using themodifier syntax.

The sections below list all available regex syntaxes, grouped by their syntactic nature.

Assertions

Assertions are constructs that test whether the string meets a certain condition at the specified position, but not consume characters. Assertions cannot bequantified.

Input boundary assertion:^,$

Asserts that the current position is the start or end of input, or start or end of a line if them flag 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:\b,\B

Asserts that the current position is a word boundary.

Atoms

Atoms are the most basic units of a regular expression. Each atomconsumes one or more characters in the string, and either fails the match or allows the pattern to continue matching with the next atom.

Backreference:\1,\2

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. When thev flag is enabled, it can also be used to match finite-length strings.

Character class escape:\d,\D,\w,\W,\s,\S

Matches any character in or not in a predefined set of characters.

Character escape:\n,\u{...}

Matches a character that may not be able to be conveniently represented in its literal form.

Literal character:a,b

Matches a specific character.

Modifier:(?ims-ims:...)

Overrides flag settings in a specific part of a regular expression.

Named backreference:\k<name>

Matches a previously matched subpattern captured with a named capturing group.

Named capturing group:(?<name>...)

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:\p{...},\P{...}

Matches a set of characters specified by a Unicode property. When thev flag is enabled, it can also be used to match finite-length strings.

Wildcard:.

Matches any character except line terminators, unless thes flag is set.

Other features

These features do not specify any pattern themselves, but are used to compose patterns.

Disjunction:|

Matches any of a set of alternatives separated by the| character.

Quantifier:*,+,?,{n},{n,},{n,m}

Matches an atom a certain number of times.

Escape sequences

Escape sequences in regexes refer to any kind of syntax formed by\ followed by one or more characters. They may serve very different purposes depending on what follow\. Below is a list of all valid "escape sequences":

Escape sequenceFollowed byMeaning
\BNoneNon-word-boundary assertion
\DNoneCharacter class escape representing non-digit characters
\P{, a Unicode property and/or value, then}Unicode character class escape representing characters without the specified Unicode property
\SNoneCharacter class escape representing non-white-space characters
\WNoneCharacter class escape representing non-word characters
\bNoneWord boundary assertion; insidecharacter classes, represents U+0008 (BACKSPACE)
\cA letter fromA toZ ora tozAcharacter escape representing the control character with value equal to the letter's character value modulo 32
\dNoneCharacter class escape representing digit characters (0 to9)
\fNoneCharacter escape representing U+000C (FORM FEED)
\k<, an identifier, then>Anamed backreference
\nNoneCharacter escape representing U+000A (LINE FEED)
\p{, a Unicode property and/or value, then}Unicode character class escape representing characters with the specified Unicode property
\q{, a string, then a}Only valid insidev-mode character classes; represents the string to be matched literally
\rNoneCharacter escape representing U+000D (CARRIAGE RETURN)
\sNoneCharacter class escape representing whitespace characters
\tNoneCharacter escape representing U+0009 (CHARACTER TABULATION)
\u4 hexadecimal digits; or{, 1 to 6 hexadecimal digits, then}Character escape representing the character with the given code point
\vNoneCharacter escape representing U+000B (LINE TABULATION)
\wNoneCharacter class escape representing word characters (A toZ,a toz,0 to9,_)
\x2 hexadecimal digitsCharacter escape representing the character with the given value
\0NoneCharacter escape representing U+0000 (NULL)

\ followed by0 and another digit becomes alegacy octal escape sequence, which is forbidden inUnicode-aware mode.\ followed by any other digit sequence becomes abackreference.

In addition,\ can be followed by some non-letter-or-digit characters, in which case the escape sequence is always acharacter escape representing the escaped character itself:

The otherASCII characters, namely space character,",',_, and any letter character not mentioned above, are not valid escape sequences. InUnicode-unaware mode, escape sequences that are not one of the above becomeidentity escapes: they represent the character that follows the backslash. For example,\a represents the charactera. This behavior limits the ability to introduce new escape sequences without causing backward compatibility issues, and is therefore forbidden in Unicode-aware mode.

Specifications

Specification
ECMAScript® 2026 Language Specification
# prod-Atom
ECMAScript® 2026 Language Specification
# prod-Assertion
ECMAScript® 2026 Language Specification
# prod-CharacterClassEscape
ECMAScript® 2026 Language Specification
# prod-CharacterClass
ECMAScript® 2026 Language Specification
# prod-Disjunction
ECMAScript® 2026 Language Specification
# prod-Quantifier
ECMAScript® 2026 Language Specification
# prod-DecimalEscape
ECMAScript® 2026 Language Specification
# prod-AtomEscape
ECMAScript® 2026 Language Specification
# prod-RegularExpressionModifiers
ECMAScript® 2026 Language Specification
# prod-PatternCharacter
ECMAScript® 2026 Language Specification
# prod-CharacterEscape

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp