Unescape html character entities

user1382306 picture user1382306 · Apr 22, 2013 · Viewed 9k times · Source

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?

Answer

Ian picture Ian · Apr 22, 2013

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/