1
html.replace(/&lt;/g, "<").replace(/&gt;/g, ">"

is there any JavaScript function can implement this function, seems like unescape function or escape function, which can convert displaying html to actual html.

so is there any function can do this kind of replace, to fulfill similar function, (i know i need to convert other things besides sign "<" ">")

Hypnoz
  • 1,115
  • 4
  • 15
  • 27

2 Answers2

0

In JavaScript running in a browser, the simplest way is to put the text into an HTML element as HTML, then take it back out as plaintext. Here are functions to do the conversion both ways:

function htmlentitydecode(html) {
    var div = document.createElement("div");
    div.innerHTML = html;
    return div.innerText || div.textContent;
}

function htmlentityencode(text) {
    var div = document.createElement("div");
    div.innerText = div.textContent = text;
    return div.innerHTML;
}

Note that the htmlentitydecode function above will also strip HTML tags. If you don't want that, then I suggest encoding, replacing "&" with "&", then decoding.

Brilliand
  • 13,404
  • 6
  • 46
  • 58
  • in jQuery, the above functions can be replaced with `$("
    ").html(html_to_decode).text()` and `$("
    ").text(text_to_encode).html()`, respectively.
    – Brilliand May 21 '12 at 03:27
0

unfortunately there is no built-in javascript function for that try to check this link HtmlSpecialChars equivalent in Javascript?

Community
  • 1
  • 1
Christopher Pelayo
  • 792
  • 11
  • 30