javascripthtmlescapingcharacter-entities

Unescape html character entities


I'm trying to store values from a database into HTML5 data attributes.

I can escape them fine because of this answer, but how do I reverse that?


Solution

  • Just reverse the function:

    function unescapeHtml(unsafe) {
        return unsafe
            .replace(/&/g, "&")
            .replace(/&lt;/g, "<")
            .replace(/&gt;/g, ">")
            .replace(/&quot;/g, "\"")
            .replace(/&#039;/g, "'");
    }
    

    DEMO: http://jsfiddle.net/wazXb/