我正在尝试使用 jsfiddle 在 Douglas Crockfords J-TBP 书中执行那个非常好的去实体化示例
String.method('deentityify', function() {
// The entity table. It maps entity names to
// characters.
var entity = {
quot: '"',
lt: '<',
gt: '>'
};
// Return the deentityify method.
return function() {
// This is the deentityify method. It calls the string
// replace method, looking for substrings that start
// with '&' and end with ';'. If the characters in
// between are in the entity table, then replace the
// entity with the character from the table. It uses
// a regular expression (Chapter 7).
return this.replace(/&([^&;]+);/g, function(a, b) {
var r = entity[b];
return typeof r === 'string' ? r : a;
});
};
}());
document.writeln('<">'.deentityify()); // <">
- 请参阅http://jsfiddle.net/cspeter8/7Pjzs/2/ - 它不起作用!我希望结果窗格显示 '<">' 但什么也没有显示。有人想解开这个谜吗?