質問

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?

役に立ちましたか?

解決

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/

他のヒント

To handle all potential characters (instead of a "known" list), use the natural escaping of the browser by letting it convert HTML strings to text with this:

function unescapeHTML(string) {
   var elt = document.createElement("span");
   elt.innerHTML = string;
   return elt.innerText;
}

References:

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top