RegExp static properties
RegExp の静的プロパティ(RegExp.$1, RegExp.lastMatch 等)は最後に実行された正規表現マッチの結果にアクセスするレガシー機能です。
概要
RegExp の静的プロパティ(RegExp.$1, RegExp.lastMatch 等)は最後に実行された正規表現マッチの結果にアクセスするレガシー機能です。
対応ブラウザ
| 機能 | デスクトップ | モバイル | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 1 | 12 | 1 | 3 | 18 | 1 | |
| ビルトインオブジェクト | ||||||
RegExp.lastMatch 非推奨 RegExp.lastMatch は静的アクセサープロパティで、最後に一致した部分文字列を返します。RegExp["$&"] はこのプロパティの別名です。 | 1 | 12 | 1 | 3 | 18 | 1 |
RegExp.lastParen 非推奨 RegExp.lastParen は静的アクセサープロパティで、最後に一致した括弧で括られた部分文字列があれば返します。RegExp["$+"] はこのプロパティの別名です。 | 1 | 12 | 1 | 3 | 18 | 1 |
| RegExp.leftContext は静的アクセサープロパティで、最近一致した文字列の前の部分文字列を返します。`RegExp["$"]`` はこのプロパティの別名です。 | 1 | 12 | 1 | 3 | 18 | 1 |
RegExp.n 非推奨 RegExp.$1, …, RegExp.$9 は静的アクセサープロパティで、括弧で括られた部分文字列に一致したものを返します。 | 1 | 12 | 1 | 1 | 18 | 1 |
| RegExp.rightContext は静的アクセサープロパティで、最新の一致に続く部分文字列が入ります。RegExp["$'"] はこのプロパティの別名です。 | 1 | 12 | 1 | 3 | 18 | 1 |
基本構文
// RegExp static properties example
// See MDN Web Docs for details ライブデモ
Read capture group shortcuts
Use RegExp.$1 and RegExp.$2 after a successful match.
Inspect match context
Read leftContext, rightContext, and lastMatch after matching text.
Show the last captured input
Demonstrate the legacy static RegExp state shared across matches.
実務での使いどころ
-
RegExp static properties の活用
RegExp の静的プロパティ($1〜$9 等)。最後にマッチした結果にアクセスする非推奨機能。
注意点
- 一部のブラウザでのみ対応。使用前にブラウザサポートを確認すること。
アクセシビリティ
- JavaScript による動的更新時は、aria-live リージョンで変更をスクリーンリーダーに通知する。
参考リンク
Powered by web-features