RegExp.prototype.compile()

Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

注意:仅出于兼容性原因指定 compile() 方法。使用 compile() 会导致原本不可变的正则表达式源和标志变得可变,这可能会打破用户的期望。你可以使用 RegExp() 构造函数来构造新的正则表达式对象。

¥Note: The compile() method is only specified for compatibility reasons. Using compile() causes the otherwise immutable regex source and flags to become mutable, which may break user expectations. You can use the RegExp() constructor to construct a new regular expression object instead.

RegExp 实例的 compile() 方法用于在创建 RegExp 对象后使用新源和标志重新编译正则表达式。

¥The compile() method of RegExp instances is used to recompile a regular expression with new source and flags after the RegExp object has already been created.

语法

¥Syntax

js
compile(pattern, flags)

参数

¥Parameters

pattern

正则表达式的文本。

flags

标志值 的任意组合。

返回值

¥Return value

无 (undefined)。

¥None (undefined).

示例

¥Examples

使用编译()

¥Using compile()

以下示例演示如何使用新模式和新标志重新编译正则表达式。

¥The following example shows how to recompile a regular expression with a new pattern and a new flag.

js
const regexObj = new RegExp("foo", "gi");
regexObj.compile("new foo", "g");

规范

Specification
ECMAScript Language Specification
# sec-regexp.prototype.compile

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also