1 Function.prototype.method =
function(name, func) {
2 if (!
this.prototype[name]) {
3 this.prototype[name] =
func;
4 }
5 };
6
7 String.method('deentityify',
function() {
8 //字符实体表。它映射字符实体的名字到对应的字符。
9 var entity =
{
10 quot: '"'
,
11 lt: '<'
,
12 gt: '>'
13 };
14
15 //返回 deentityify 方法
16 return function() {
17 //这才是 deentityify 方法。它调用字符串的 replace 方法,查找'&'开头和';'结束的子字符串。如果这些字符可以再字符实体表中找到,那么就将该字符实体替换为映射表中的值。
18 return this.replace(/&([^&;]+);/g,
function(a, b) {
19 var r =
entity[b];
20 return typeof r === 'string' ?
r : a;
21 });
22 };
23 }());
24
25 document.writeln('<">'.deentityify());
链接:HTML 字符实体
转载于:https://www.cnblogs.com/qzsonline/archive/2012/07/11/2586084.html