What does RegExp.$1 do

后端 未结 6 866
温柔的废话
温柔的废话 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 13:58

    $1 is whatever is matched in the first capture. If you have more captures you can use $2, $3 etc.

    Ex:

    "abc".replace(/(.)/, "$1$1"); // aabc
    "abc".replace(/(.{2})/, "$1$1"); // ababc
    "abc".replace(/(.)(.)/, "$2$1"); // bac
    
    0 讨论(0)
  • 2021-02-02 13:59

    Like stated earlier, these constructs are called capturing parentheses groups and they're primarily used to reference the matches saved/remembered following the successful execution of an RegExp related operation.

    Also, like stated earlier, I don't advise using any of them in your code unless a well controlled environment/context is secured, like the one provided by the native replace() method found on the String.prototype object.

    0 讨论(0)
  • 2021-02-02 14:02

    $1 will return the first group that matches a regular expression.

    In your example, the value stored in $1 is whatever was matching .+

    The group is denoted by parenthesis and you can have multiples. Each saved group will just increment the digit with the $, i.e. $1, $2, $3...

    Example:

    If your input was __product[Version 12 Coupe] then $1 would contain Version 12 Coupe

    0 讨论(0)
  • 2021-02-02 14:02

    These work in conjunction with capturing parentheses. For example, /(foo)/ matches and remembers 'foo' in "foo bar." The matched substring can be recalled from the resulting array's elements [1], ..., [n] or from the predefined RegExp object's properties $1, ..., $9.

    In your example, the $1 refers to the match made by (.+)

    See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

    0 讨论(0)
  • 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)
    0 讨论(0)
  • 2021-02-02 14:15

    The literal expression RegExp.$1 will get you the value of the first capture group of the last regex ran. Whatever that regex was.

    For example:

    var match = /_(.*)_/.exec('_test_');
    
    var newMatch = '123-abc'.match(/(\d*)-(\w*)/);
    var num = RegExp.$1; // '123';
    

    RegExp.$1 is globally available, so it can be accessed from anywhere in your page, regardless of where the regex itself was ran.

    I've never seen this syntax used before seeing this question, and I wouldn't suggest using it, as I cannot find documentation on it. Also, any regex ran on your page, regardless of where, will modify this property. If you want to get the capture groups, I'd use the arrays returned from String.match or RegExp.exec instead.

    EDIT: I found some documentation about this: http://msdn.microsoft.com/en-us/library/ie/24th3sah(v=vs.94).aspx

    EDIT 2: I found some more info about this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features#RegExp_Properties

    RegExp.$1 is deprecated. That means future browsers might remove this "feature", so I suggest not using it.

    0 讨论(0)
提交回复
热议问题