在jsfiddle上无法执行Doug Crockford的deentityify示例。

4

我正在尝试使用jsfiddle执行Douglas Crockford的J-TBP书中非常好的deentityify示例,该示例涉及it技术。

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('&lt;&quot;&gt;'.deentityify()); // <">          
1个回答

8

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接