Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams num.replace(re, "$1,$2");

I could not understand the grammar of "$1,$2" . The book from which this code comes says $1 means RegExp.$1 , $2 means RegExp.$2 . But these explanations lead to more questions:

  • 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?

  • If I input $1 , the command line says it is not defined; if I input "$1" , the command line only echoes $1 , not 11222. So, how does the replace method know what "$1,$2" mean?

    Thank you.

    When it doubt, read the documentation : "The replacement string can include the following special replacement patterns .." ( $1 is a $n replacement pattern). Also see string literals - JavaScript has no "interpolation" as found in PHP or Perl. user2246674 May 22, 2013 at 23:01 In a language with interpolation, $name = "Fred"; print "Hello $name!" might result in "Hello Fred!". One text-book definition of interpolation is to "Insert (something) between fixed points" and in computer languages this often means "Insert value of a variable/expression into a string". JavaScript does not have this feature. Because this is not present in JavaScript it can be concluded that "$1,$2" results in a string of 5 characters and does not interpolate values from any variables - the special handling is then from the String.replace function (which I also linked). user2246674 May 22, 2013 at 23:24

    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"));
    Thank you. but what about my question (1), RegExt.$1? When I input RegExp.$1, the command line returns 11222. So $1 seems to be a member variable of object RegExt. How could $1 be a name of a variable? user2384994 May 22, 2013 at 23:13 @user2384994: If you mean you replaced the replace pattern with RegExp.$1 , then it should return RegExp.11222 . As far as it goes, though, JavaScript variables can start with $ . That's all there is to it. voithos May 22, 2013 at 23:32 In JavaScript, how can be $1 used in replacement string when it is followed by number (e.g. 0 )? Syntax $10 is wrong. Please advise. Ωmega Sep 7, 2017 at 16:43

    $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.

    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.

    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.

    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.

  • 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().
  • 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!

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.

  •