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

后端 未结 5 1810
一个人的身影
一个人的身影 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: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.

提交回复
热议问题