UNB/ CS/ David Bremner/ teaching/ cs2613/ books/ mdn/ Reference/ Global Objects/ RegExp/ RegExp.lastParen ($+)

Note: All RegExp static properties that expose the last match state globally are deprecated. See deprecated RegExp features for more information.

The RegExp.lastParen static accessor property returns the last parenthesized substring match, if any. RegExp["$+"] is an alias for this property.

Description

Because lastParen is a static property of RegExp, you always use it as RegExp.lastParen or RegExp["$+"], rather than as a property of a RegExp object you created.

The value of lastParen updates whenever a RegExp (but not a RegExp subclass) instance makes a successful match. If no matches have been made, or if the most recent regex execution contains no capturing groups, lastParen is an empty string. The set accessor of lastParen is undefined, so you cannot change this property directly.

You cannot use the shorthand alias with the dot property accessor (RegExp.$+), because + is not a valid identifier part, so this causes a SyntaxError. Use the bracket notation instead.

Examples

Using lastParen and $+

const re = /(hi)/g;
re.test("hi there!");
RegExp.lastParen; // "hi"
RegExp["$+"]; // "hi"

Specifications

Browser compatibility

See also