132
rated 0 times
[
133]
[
1]
/ answers: 1 / hits: 88618
/ 11 Years ago, wed, may 22, 2013, 12:00:00
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.
More From » regex