Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. RegExp
  6. leftContext ($`)

RegExp.leftContext ($`)

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 thecompatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

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

TheRegExp.leftContext static accessor property returns the substring preceding the most recent match.RegExp["$`"] is an alias for this property.

Description

BecauseleftContext is a static property ofRegExp, you always use it asRegExp.leftContext orRegExp["$`"], rather than as a property of aRegExp object you created.

The value ofleftContext updates whenever aRegExp (but not aRegExp subclass) instance makes a successful match. If no matches have been made,leftContext is an empty string. The set accessor ofleftContext isundefined, 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 aSyntaxError. Use thebracket notation instead.

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

Examples

Using leftContext and $`

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

Specifications

Specification
Legacy RegExp features
# additional-properties-of-the-regexp-constructor

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp