为什么在将函数传递给replace
的最后一个参数时,该函数不带括号?为什么函数传递替换不带括号
从MDN:
function replacer(match, p1, p2, p3, offset, string){
// p1 is nondigits, p2 digits, and p3 non-alphanumerics
return [p1, p2, p3].join(' - ');
};
newString = "abc12345#$*%".replace(/([^\d]*)(\d*)([^\w]*)/, replacer);
而不是..? – 1252748
这是相反的,而不是传递一个函数的参考 –