参考:
JS转换HTML转义符
Decode & back to & in JavaScript

还是stackoverflow上的回答比较专业

var encodedStr = 'hello & world';

var parser = new DOMParser;
var dom = parser.parseFromString(
    '<!doctype html><body>' + encodedStr,
    'text/html');
var decodedString = dom.body.textContent;

console.log(decodedString);

借此写个函数

function decoder(str){
    var parser = new DOMParser;
    var dom = parser.parseFromString(
        '<!doctype html><body>' + str,
        'text/html');
    var decodedString = dom.body.textContent;
    return decodedString;
}

标签: none 阅读量: 1635

添加新评论