RegExp() 构造函数

RegExp() 构造函数创建 RegExp 对象。

¥The RegExp() constructor creates RegExp objects.

有关正则表达式的介绍,请阅读 JavaScript 指南 中的 正则表达式章节

¥For an introduction to regular expressions, read the Regular Expressions chapter in the JavaScript Guide.

Try it

语法

¥Syntax

js
new RegExp(pattern)
new RegExp(pattern, flags)
RegExp(pattern)
RegExp(pattern, flags)

注意:RegExp() 可以与 new 一起调用,也可以不与 new 一起调用,但有时效果不同。参见 返回值

¥Note: RegExp() can be called with or without new, but sometimes with different effects. See Return value.

参数

¥Parameters

pattern

正则表达式的文本。这也可以是另一个 RegExp 对象。

flags Optional

如果指定,flags 是一个包含要添加的标志的字符串。或者,如果为 pattern 提供了 RegExp 对象,则 flags 字符串将替换该对象的任何标志(并且 lastIndex 将重置为 0)。

flags 可以包含以下字符的任意组合:

d(指数)

生成子字符串匹配的索引。

g(全球的)

查找所有匹配项,而不是在第一个匹配项后停止。

i(忽略大小写)

匹配时,忽略大小写差异。

m(多行)

将开始和结束断言(^$)视为在多行上工作。换句话说,匹配每行的开头或结尾(由 \n\r 分隔),而不仅仅是整个输入字符串的开头或结尾。

s(点全部)

允许 . 匹配换行符。

u(统一码)

pattern 视为 Unicode 代码点序列。

v(unicode 集)

u 标志的升级,可在字符类以及字符串属性中启用集合表示法。

y(黏)

仅从目标字符串中该正则表达式的 lastIndex 属性指示的索引开始匹配。不尝试从任何后续索引进行匹配。

返回值

¥Return value

如果满足以下所有条件,则 RegExp(pattern) 直接返回 pattern

¥RegExp(pattern) returns pattern directly if all of the following are true:

  • RegExp() 在没有 new 的情况下被调用;
  • pattern 是一个正则表达式
  • pattern.constructor === RegExp(通常意味着它不是子类);
  • flagsundefined

在所有其他情况下,调用 RegExp() 并使用或不使用 new 都会创建一个新的 RegExp 对象。如果 pattern 是正则表达式,则新对象的 sourcepattern.source;否则,其来源为 pattern 强制为字符串。如果 flags 参数不是 undefined,则新对象的 flags 就是该参数的值;否则,其 flagspattern.flags(如果 pattern 是正则表达式)。

¥In all other cases, calling RegExp() with or without new both create a new RegExp object. If pattern is a regex, the new object's source is pattern.source; otherwise, its source is pattern coerced to a string. If the flags parameter is not undefined, the new object's flags is the parameter's value; otherwise, its flags is pattern.flags (if pattern is a regex).

例外情况

¥Exceptions

SyntaxError

有下列情况之一的,抛出:

  • pattern 无法解析为有效的正则表达式。
  • flags 包含重复字符或允许范围之外的任何字符。

示例

¥Examples

文字表示法和构造函数

¥Literal notation and constructor

创建 RegExp 对象有两种方法:文字符号和构造函数。

¥There are two ways to create a RegExp object: a literal notation and a constructor.

  • 字面表示法采用两个斜杠之间的模式,第二个斜杠后面紧跟可选标志。
  • 构造函数将字符串或 RegExp 对象作为其第一个参数,并将可选标志字符串作为其第二个参数。

以下三个表达式创建相同的正则表达式:

¥The following three expressions create the same regular expression:

js
/ab+c/i;
new RegExp(/ab+c/, "i"); // literal notation
new RegExp("ab+c", "i"); // constructor

在使用正则表达式之前,必须先对其进行编译。这个过程使他们能够更有效地进行比赛。有两种方法可以编译并获取 RegExp 对象。

¥Before regular expressions can be used, they have to be compiled. This process allows them to perform matches more efficiently. There are two ways to compile and get a RegExp object.

当计算表达式时,文字表示法会导致正则表达式的编译。另一方面,RegExp 对象 new RegExp('ab+c') 的构造函数导致正则表达式的运行时编译。

¥The literal notation results in compilation of the regular expression when the expression is evaluated. On the other hand, the constructor of the RegExp object, new RegExp('ab+c'), results in runtime compilation of the regular expression.

当你想要 从动态输入构建正则表达式 时,请使用字符串作为 RegExp() 构造函数的第一个参数。

¥Use a string as the first argument to the RegExp() constructor when you want to build the regular expression from dynamic input.

从动态输入构建正则表达式

¥Building a regular expression from dynamic inputs

js
const breakfasts = ["bacon", "eggs", "oatmeal", "toast", "cereal"];
const order = "Let me get some bacon and eggs, please";

order.match(new RegExp(`\\b(${breakfasts.join("|")})\\b`, "g"));
// Returns ['bacon', 'eggs']

规范

Specification
ECMAScript Language Specification
# sec-regexp-constructor

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看