A piece of JavaScript code is as follows:
num = "11222333";
re = /(d+)(d{3})/;
re.test(num);
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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…