String.method('deentityify', function (){
    var entity = {
        quot: '"',
        lt: '<',
        gt: '>'
    };
    return function(){
        return this.replace(/&([^&;]+);/g,
            function(a, b){
                var r = entity[b];
                return typeof r === 'string' ? r : a;
            }
        );
    };
}());
alert('&lt;&quot;&gt;'.deentityify());这是书上看到的,但怎会跑不出结果

解决方案 »

  1.   

    String.method = function(a,b){
    String.prototype[a] = b();
    }
    String.method('deentityify', function (){
        var entity = {
            quot: '"',
            lt: '<',
            gt: '>'
        };
        return function(){
            return this.replace(/&([^&;]+);/g,
                function(a, b){
                    var r = entity[b];
                    return typeof r === 'string' ? r : a;
                }
            );
        };
    });alert('&lt;&quot;&gt;'.deentityify());