Given this 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());
What goes into the last function as a and b, and why?
I get that we’re splitting up the string I’m passing in into 3 groups, but I don’t understand why < is going into a and why just lt is going into b.
The first argument contains the whole match, all consecutive arguments are the matched groups. The last two arguments are the offset and full input string.
The pattern matches all occurrences of
&+ everynon-&+;.See also: MDN:
String.replacewith a function