1

Yesterday I was answering a question on stackoverflow, and there's something I don't understand in my own answer...

References: the thread in question, and my fiddle

Here is the code from my answer:

var rx = /{([0-9]+)}/g;
str=str.replace(rx,function($0,$1){return params[parseInt($1)];});

Now, what surprises me is that the following code works too:

var rx = /{([0-9]+)}/g;
str=str.replace(rx,function($0,$1){return params[$1];});

My question: how come parseInt is not needed? At what point does JavaScript convert $1 into a number? Is it in the regex, or in the array?

4

1 に答える 1

0

これは、javascriptがインデックスを文字列として読み取るためです

array[1]array['1']、読み取られる前にに変換されます

同じようobject['first']に動作します

于 2012-07-12T16:56:24.903 に答える