RegExp
constructor creates a regular expression object for matching text with a pattern.
For an introduction to regular expressions, read the Regular Expressions chapter 在 JavaScript 指南 .
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Literal, constructor, and factory notations are possible:
/pattern/flags new RegExp(pattern[, flags]) RegExp(pattern[, flags])
pattern
The text of the regular expression.
RegExp
object or literal (for the two RegExp constructor notations only). Patterns may include
special characters
to match a wider range of values than would a literal string.
flags
If specified,
flags
is a string that contains the flags to add.
Alternatively, if an object is supplied for the pattern, the
flags
string will replace any of that object's flags (and
lastIndex
will be reset to
0
) (as of ES2015).
若
flags
is not specified and a regular expressions object is supplied, that object's flags (and
lastIndex
value) will be copied over.
flags
may contain any combination of the following characters:
g
(global match)
Find all matches rather than stopping after the first match.
i
(ignore case)
u
flag is also enabled, use Unicode case folding.
m
(multiline)
^
and
$
) as working over multiple lines. In other words, match the beginning or end of
each
line (delimited by
\n
or
\r
), not only the very beginning or end of the whole input string.
s
("dotAll")
.
to match newlines.
u
(unicode)
pattern
as a sequence of Unicode code points. (See also
二进制字符串
).
y
(sticky)
lastIndex
property of this regular expression in the target string. Does not attempt to match from any later indexes.
There are two ways to create a
RegExp
object: a
literal notation
和
构造函数
.
The following three expressions create the same regular expression:
/ab+c/i
new RegExp(/ab+c/, 'i') // literal notation
new RegExp('ab+c', 'i') // constructor
The literal notation results in compilation of the regular expression when the expression is evaluated. Use literal notation when the regular expression will remain constant. For example, if you use literal notation to construct a regular expression used in a loop, the regular expression won't be recompiled on each iteration.
The constructor of the regular expression object—for example,
new RegExp('ab+c')
—results in runtime compilation of the regular expression. Use the constructor function when you know the regular expression pattern will be changing, or you don't know the pattern and are getting it from another source, such as user input.
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'RegExp constructor' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
RegExp()
构造函数
|
Chrome 1 | Edge 12 | Firefox 1 | IE 4 | Opera 5 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs Yes |
完整支持
String.prototype.match()
String.prototype.replace()
RegExp
RegExp.$1-$9
RegExp.input ($_)
RegExp.lastMatch ($&)
RegExp.lastParen ($+)
RegExp.leftContext ($`)
RegExp.prototype.dotAll
RegExp.prototype.flags
RegExp.prototype.global
RegExp.prototype.ignoreCase
RegExp.prototype.multiline
RegExp.prototype.source
RegExp.prototype.sticky
RegExp.prototype.unicode
RegExp.rightContext ($')
RegExpInstance.lastIndex
get RegExp[@@species]
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()