--- title: RegExp.lastMatch ($&) slug: Web/JavaScript/Reference/Global_Objects/RegExp/lastMatch tags: - JavaScript - Non-standard - Property - Read-only - Reference - RegExp - Regular Expressions translation_of: Web/JavaScript/Reference/Global_Objects/RegExp/lastMatch ---
標準外の lastMatch プロパティは、最後に一致した文字列を含む正規表現の静的で読み取り専用のプロパティです。 RegExp.$& はこのプロパティに対するエイリアスです。
lastMatch プロパティは静的プロパティです。個々の正規表現オブジェクトのプロパティではありません。そのかわりに、常に、RegExp.lastMatchまたはRegExp['$&'].として使用してください。
lastMatch プロパティの値は、読み取り専用で、一致に成功するたびに変更されます。
ドットプロパティアクセサー (RegExp.$&) で短縮エイリアスを使用することはできません。その場合、パーサーは "&" という表現を期待して {{jsxref("SyntaxError")}} が発生します。プロパティへのアクセスにはブラケット表記法を使用してください。
var re = /hi/g;
re.test('hi there!');
RegExp.lastMatch; // "hi"
RegExp['$&']; // "hi"
| 仕様書 |
|---|
| {{SpecName('Legacy RegExp features')}} |
{{Compat("javascript.builtins.RegExp.lastMatch")}}