Limited supportAvoid in new code. Explicit match results are clearer, safer, and easier to maintain.

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
1
12
1
3
18
1
Built-in object

The RegExp.lastMatch static accessor property returns the last matched substring. RegExp["$&"] is an alias for this property.

1
12
1
3
18
1

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

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

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
1+Supported (version) Not supported Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)

Syntax

JAVASCRIPT
// 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.

JavaScript
Output
Press the Run button

Inspect match context

Read leftContext, rightContext, and lastMatch after matching text.

JavaScript
Output
Press the Run button

Show the last captured input

Demonstrate the legacy static RegExp state shared across matches.

JavaScript
Output
Press the Run button

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.

Powered by web-features