--- title: RegExp.lastMatch ($&) slug: Web/JavaScript/Reference/Global_Objects/RegExp/lastMatch translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/lastMatch ---
lastMatch 非标准属性是正则表达式的静态和只读属性,含有最后匹配到的字符串。RegExp.$& 是这个属性的别名。
RegExp.lastMatch RegExp['$&']
lastMatch 属性是静态的,不是正则表达式独立对象的属性。反之,你应始终将其使用为 RegExp.lastMatch 或者 RegExp['$&']。
lastMatch 属性的值是只读的,并且会在匹配成功时修改。
你不能使用属性访问器(RegExp.$&)来使用简写的别名,因为解析器在这里会将 "&" 看做表达式,并抛出 {{jsxref("SyntaxError")}} 。使用 方括号符号来访问属性。
lastMatch 和 $&var re = /hi/g;
re.test('hi there!');
RegExp.lastMatch; // "hi"
RegExp['$&']; // "hi"
非标准。并不是任何现行规范的一部分。