How to explain “$1,$2” in Javascript when using regular expression?

后端 未结 5 1793
一个人的身影
一个人的身影 2020-12-07 23:11

A piece of Javascript code is as follows:

    num=\"11222333\";
    re = /(\\d+)(\\d{3})/;
    re.test(num);
    num.replace(re, \"$1,$2\");
<
相关标签:
5条回答
  • 2020-12-07 23:42

    $1 is the first group from your regular expression, $2 is the second. Groups are defined by brackets, so your first group ($1) is whatever is matched by (\d+). You'll need to do some reading up on regular expressions to understand what that matches.

    It is known that in Javascript, the name of variables should begin with letter or _, how can $1 be a valid name of member variable of RegExp here?

    This isn't true. $ is a valid variable name as is $1. You can find this out just by trying it. See jQuery and numerous other frameworks.

    0 讨论(0)
  • 2020-12-07 23:45

    The book from which this code comes says $1 means RegExp.$1, $2 means RegExp.$2.

    This book is made of paper. And paper cannot oppose any resistance to whom is writing on it :-) . But perhaps did you only misinterpret what is actually written in this book.

    Actually, it is depending on the context.

    1. In the context of the replace() method of String, $1, $2, ... $99 (1 through 99) are placeholders. They are handled internally by the replace() method (and they have nothing to do with RegExp.$1, RegExp.$2, etc, which are probably not even defined (see point 2. )). See String.prototype.replace() #Specifying_a_string_as_a_parameter. Compare this with the return value of the match() method of String when the flag g is not used, which is similar to the return value of the exec() method of RegExp. Compare also with the arguments passed implicitly to an (optional) function specified as second argument of replace().
    2. RegExp.$1, RegExp.$2, ... RegExp.$9 (1 through 9 only) are non-standard properties of RegExp as you may see at RegExp.$1-$9 and Deprecated and obsolete features. They seem to be implemented on your browser, but, for somebody else, they could be not defined. To use them, you need always to prepend $1, $2, etc with RegExp.. These properties are static, read-only and stored in the RegExp global object, not in an individual regular expression object. But, anyway, you should not use them. The $1 through $99 used internally by the replace() method of String are stored elsewhere.

    Have a nice day!

    0 讨论(0)
  • 2020-12-07 23:47

    It is known that in Javascript, the name of variables should begin with letter or _,

    No, it's not. $1 is a perfectly valid variable. You have to assign to it first though:

    $variable = "this is a test"
    

    This is how jQuery users a variable called $ as an alias for the jQuery object.

    0 讨论(0)
  • 2020-12-08 00:01

    You are misinterpreting that line of code. You should consider the string "$1,$2" a format specifier that is used internally by the replace function to know what to do. It uses the previously tested regular expression, which yielded 2 results (two parenthesized blocks), and reformats the results. $1 refers to the first match, $2 to the second one. The expected contents of the num string is thus 11222,333 after this bit of code.

    0 讨论(0)
  • 2020-12-08 00:02

    It's not a "variable" - it's a placeholder that is used in the .replace() call. $n represents the nth capture group of the regular expression.

    var num = "11222333";
    
    // This regex captures the last 3 digits as capture group #2
    // and all preceding digits as capture group #1
    var re = /(\d+)(\d{3})/;
    
    console.log(re.test(num));
    
    // This replace call replaces the match of the regex (which happens
    // to match everything) with the first capture group ($1) followed by
    // a comma, followed by the second capture group ($2)
    console.log(num.replace(re, "$1,$2"));

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