W3cubDocs

/JavaScript

RegExp.rightContext ($')

Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

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

The RegExp.rightContext static accessor property returns the substring following the most recent match. RegExp["$'"] is an alias for this property.

Description

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

The value of rightContext updates whenever a RegExp (but not a RegExp subclass) instance makes a successful match. If no matches have been made, rightContext is an empty string. The set accessor of rightContext 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.

$' can also be used in the replacement string of String.prototype.replace(), but that's unrelated to the RegExp["$'"] legacy property.

Examples

Using rightContext and $'

js
const re = /hello/g;
re.test("hello world!");
RegExp.rightContext; // " world!"
RegExp["$'"]; // " world!"

Specifications

Browser compatibility

Desktop Mobile Server
Chrome Edge Firefox Opera Safari Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet WebView Android Deno Node.js
rightContext 1 12 1 8 3 18 4 10.1 1 1.0 4.4 1.0 0.10.0

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/rightContext