| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/leftContext | [Back] [Original] |
Get to know MDN better
Avoid using this feature in new projects.
The RegExp static properties may have unintended side effects.
This feature may be a candidate for removal from web standards or browsers.
Note:
All RegExp static properties that expose the last match state globally are deprecated. See deprecated RegExp features for more information.
The RegExp.leftContext static accessor property returns the substring preceding the most recent match. RegExp["$`"] is an alias for this property.
Because leftContext is a static property of RegExp, you always use it as RegExp.leftContext or RegExp["$`"], rather than as a property of a RegExp object you created.
The value of leftContext updates whenever a RegExp (but not a RegExp subclass) instance makes a successful match. If no matches have been made, leftContext is an empty string. The set accessor of leftContext 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.
const re = /world/g;
re.test("hello world!");
RegExp.leftContext; // "hello "
RegExp["$`"]; // "hello "
| Specification |
|---|
| Legacy RegExp features # additional-properties-of-the-regexp-constructor |
RegExp.input ($_)RegExp.lastMatch ($&)RegExp.lastParen ($+)RegExp.rightContext ($')RegExp.$1, , RegExp.$9This page was last modified on Jul 10, 2025 by MDN contributors.
RegExpObject/FunctionYour blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |