鉴于此Javascript:如何在Javascript正则表达式中分组?
String.method('deentityify', function() {
// The entity table. It maps entity names to
// characters.
var entity = {
quot: '"',
lt: '<',
gt: '>'
};
// Return the deentityify method
return function() {
return this.replace(/&([^&;]+);/g,
function (a, b) {
var r = entity[b];
return typeof r === 'string' ? r : a;
}
);
};
}());
document.writeln('<">'.deentityify());
什么进入最后一个函数A和B,为什么?
我得知我们正在将我传入的字符串分成3组,但我不明白为什么<
正在进入一个为什么只有lt
正在进入b。
看一看的[MDN文档】(https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter)。 – 2012-03-14 17:17:19