RegExp static properties
RegExp static properties such as $1 through $9 expose the latest match state globally. They are legacy features that make regular-expression code harder to reason about than explicit match objects.
Overview
RegExp static properties such as $1 through $9 expose the latest match state globally. They are legacy features that make regular-expression code harder to reason about than explicit match objects.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
javascript.builtins.RegExp.input Deprecated | 1 | 12 | 1 | 3 | 18 | 1 |
| Built-in object | ||||||
RegExp.lastMatch Deprecated The RegExp.lastMatch static accessor property returns the last matched substring. RegExp["$&"] is an alias for this property. | 1 | 12 | 1 | 3 | 18 | 1 |
RegExp.lastParen Deprecated The RegExp.lastParen static accessor property returns the last parenthesized substring match, if any. RegExp["$+"] is an alias for this property. | 1 | 12 | 1 | 3 | 18 | 1 |
RegExp.leftContext Deprecated The RegExp.leftContext static accessor property returns the substring preceding the most recent match. `RegExp["$"]`` is an alias for this property. | 1 | 12 | 1 | 3 | 18 | 1 |
RegExp.n Deprecated The RegExp.$1, …, RegExp.$9 static accessor properties return parenthesized substring matches. | 1 | 12 | 1 | 1 | 18 | 1 |
RegExp.rightContext Deprecated The RegExp.rightContext static accessor property returns the substring following the most recent match. RegExp["$'"] is an alias for this property. | 1 | 12 | 1 | 3 | 18 | 1 |
Syntax
// RegExp static properties example
// See MDN Web Docs for details Live demo
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.
Use cases
Regex cleanup
Replace global match-state access with local match objects during refactors.
Legacy parsing review
Understand older text-processing code that depends on implicit global capture values.
Cautions
- Global match state is brittle because later regex operations can overwrite the values unexpectedly.
- Local match results make validation and parsing logic far easier to test.
Accessibility
- Reliable parsing helps ensure validation messages and extracted text remain accurate for users.
- Prefer explicit parsing state wherever regex output influences accessible UI.
Related links
Powered by web-features