已废弃
该特性已经从 Web 标准中删除,虽然一些浏览器目前仍然支持它,但也许会在未来的某个时间停止支持,请尽量不要使用该特性。
已废弃的compile() 方法被用于在脚本执行过程中(重新)编译正则表达式。与RegExp构造函数基本一样。
语法
regexObj.compile(pattern, flags)
参数
-
pattern - 正则表达式的文本 。
-
flags -
如果指定,标志可以具有以下值的任意组合:
-
g - 全局匹配
-
i - 忽略大小写
-
m - 多行;让开始和结束字符(^ 和 $)工作在多行模式工作(例如,^ 和 $ 可以匹配字符串中每一行的开始和结束(行是由 \n 或 \r 分割的),而不只是整个输入字符串的最开始和最末尾处。
-
y - 黏度; 在目标字符串中,只从正则表达式的lastIndex属性指定的显示位置开始匹配(并且不试图从任何之后的索引匹配)。
-
描述
不推荐compile方法。你可以使用 RegExp 构造函数来得到相同效果。
示例
使用compile()
以下展示如何用新模式和新标志来重新编译正则表达式。
var regexObj = new RegExp("foo", "gi");
regexObj.compile("new foo", "g");
规范
| Specification | Status | Comment |
|---|---|---|
| ECMAScript 2015 (6th Edition, ECMA-262) RegExp.prototype.compile |
Standard | Initial definition. Defined in the (normative) Annex B for Additional ECMAScript Features for Web Browsers. |
| ECMAScript Latest Draft (ECMA-262) RegExp.prototype.compile |
Draft | Defined in the (normative) Annex B for Additional ECMAScript Features for Web Browsers. |
浏览器兼容性
We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains.
Find out how you can help!
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |