非标
此特征是非标准的,且不在标准轨道中。不要在面向 Web 的生产站点中使用它:它不适用于每个用户。实现之间可能存在大的不兼容性,且行为将来可能改变。

The non-standard lastMatch property is a static and read-only property of regular expressions that contains the last matched characters. RegExp.$& is an alias for this property.

描述

lastMatch property is static, it is not a property of an individual regular expression object. Instead, you always use it as RegExp.lastMatch or RegExp['$&'].

The value of the lastMatch property is read-only and modified whenever a successful match is made.

You can not use the shorthand alias with the dot property accessor ( RegExp.$& ), because the parser expects an expression with "&" in that case and a SyntaxError is thrown. Use the bracket notation for property access .

范例

Using lastMatch and $&

var re = /hi/g;
re.test('hi there!');
RegExp.lastMatch; // "hi"
RegExp['$&'];     // "hi"
					

规范

规范
Legacy RegExp features in JavaScript

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
RegExp.lastMatch ( $& ) 非标 Chrome 1 Edge 12 Firefox 1 IE 5.5 Opera 10.5 Safari 3 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 11 Safari iOS 1 Samsung Internet Android 1.0 nodejs Yes

图例

完整支持

完整支持

非标。预期跨浏览器支持较差。

另请参阅

元数据

  • 最后修改: