Unescape HTML entities in Javascript?

前端 未结 30 3545
野趣味
野趣味 2020-11-21 05:40

I have some Javascript code that communicates with an XML-RPC backend. The XML-RPC returns strings of the form:


30条回答
  •  孤城傲影
    2020-11-21 05:54

    All of the other answers here have problems.

    The document.createElement('div') methods (including those using jQuery) execute any javascript passed into it (a security issue) and the DOMParser.parseFromString() method trims whitespace. Here is a pure javascript solution that has neither problem:

    function htmlDecode(html) {
        var textarea = document.createElement("textarea");
        html= html.replace(/\r/g, String.fromCharCode(0xe000)); // Replace "\r" with reserved unicode character.
        textarea.innerHTML = html;
        var result = textarea.value;
        return result.replace(new RegExp(String.fromCharCode(0xe000), 'g'), '\r');
    }
    

    TextArea is used specifically to avoid executig js code. It passes these:

    htmlDecode('<& >'); // returns "<& >" with non-breaking space.
    htmlDecode('  '); // returns "  "
    htmlDecode(''); // Does not execute alert()
    htmlDecode('\r\n') // returns "\r\n", doesn't lose the \r like other solutions.
    

提交回复
热议问题