1這是什麼$ 3 $ 2 $ 1只表示在:的Javascript替換法澄清
var s = "The quick brown fox jumped over the lazy dog.";
var re = /(\S+)(\s+)(\S+)/g;
// Exchange each pair of words.
var result = s.replace(re, "$3$2$1");
document.write(result);
// Output: quick The fox brown over jumps lazy the dog.
2和$ 0,$ 1,$ 2:
function f2c(s1) {
// Initialize pattern.
var test = /(\d+(\.\d*)?)F\b/g;
// Use a function for the replacement.
var s2 = s1.replace(test,
function($0,$1,$2)
{
return((($1-32) * 5/9) + "C");
}
)
return s2;
}
document.write(f2c("Water freezes at 32F and boils at 212F."));
// Output: Water freezes at 0C and boils at 100C.
注意,感謝您的回覆,我明白了數字1,但數字2有點困難,我正在努力解決它。
感謝,
優素福
[Submatch replacement pattern](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_string_as_a_parameter)。 – 2014-09-23 14:54:45