What does RegExp.$1 do

后端 未结 6 873
温柔的废话
温柔的废话 2021-02-02 13:34

I have come across a piece of code in JScript:

RegExp.$1

Does anybody know what it does?

If I output it on its own, I get nothing not e

6条回答
  •  灰色年华
    2021-02-02 14:06

    To add some details:

    (As already said,) the RegExp.$n-property (where n is a single digit 1-9) returns the last nth parenthesized (captured) substring in a match.

    These properties were first implemented in JavaScript 1.2 and deprecated in JavaScript 1.5 - when RegExp underwent a major change and many of the results from RegExp.prototype.exec(string) were moved from the RegExp object to the RegExp instance and all the .$ properties (and their full name versions (except for .multiline)) "went away".


    The non-standard1 $1, $2, $3, $4, $5, $6, $7, $8, $9 properties are static and read-only properties of regular expressions (that contain parenthesized substring matches) and are modified whenever successful matches are made.

    They are not a property of an individual regular expression object. Instead, you always use them as RegExp.$1, ..., RegExp.$9.

    The number of possible parenthesized substrings is unlimited (of course), but the RegExp object can only hold the last nine.

    1 Non-standard = Not part of any current specification!


    You can find the definition and references in the following sections of the ECMA-262 3 Specs:

    • 15.5.4.10 - String.prototype.match(regexp)
    • 15.5.4.11 - String.prototype.replace(regexp)
    • 15.10.2.1 - The RegExp Object Notation's NCapturingParens
    • 15.10.6.2 - RegExp.prototype.exec(string)

提交回复
热议问题