RegExp.lastMatch ($&)

非标准: 该特性是非标准的,请尽量不要在生产环境中使用它!

lastMatch 非标准属性是正则表达式的静态和只读属性,含有最后匹配到的字符串。RegExp.$& 是这个属性的别名。

语法

RegExp.lastMatch
RegExp['$&']

描述

lastMatch 属性是静态的,不是正则表达式独立对象的属性。反之,你应始终将其使用为 RegExp.lastMatch 或者 RegExp['$&']

lastMatch 属性的值是只读的,并且会在匹配成功时修改。

你不能使用属性访问器 (RegExp.$&) 来使用简写的别名,因为解析器在这里会将 "&" 看做表达式,并抛出 SyntaxError 。使用 方括号符号来访问属性。

示例

使用 lastMatch$&

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

规范

非标准。并不是任何现行规范的一部分。

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
RegExp.lastMatch ($&)
Deprecated

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Deprecated. Not for use in new websites.

参见