开发者

Decode HTML entities in JavaScript? [duplicate]

开发者 https://www.devze.com 2022-12-30 01:24 出处:网络
This question already has answers here: Unescape HTML entities in JavaScript? (32 answers) Closed 7 开发者_如何学JAVAmonths ago.
This question already has answers here: Unescape HTML entities in JavaScript? (32 answers) Closed 7 开发者_如何学JAVAmonths ago.

Sample conversions:

 & -> `&`
 >  -> `>`

Any small library function that can handle this?


I have on my utility belt this tiny function always:

function htmlDecode(input){
  var e = document.createElement('div');
  e.innerHTML = input;
  return e.childNodes[0].nodeValue;
}

htmlDecode("&"); // "&"
htmlDecode(">"); // ">"

It will work for all HTML Entities.

Edit: Since you aren't in a DOM environment, I think you will have to do it by the "hard" way:

function htmlDecode (input) {
  return input.replace(/&/g, "&")
              .replace(/&lt;/g, "<")
              .replace(/&gt;/g, ">");
              //...
}

If you don't like the chained replacements, you could build an object to store your entities, e.g.:

function htmlDecode (input) {
  var entities= {
    "&amp;": "&",
    "&lt;": "<",
    "&gt;": ">"
    //....
  };

  for (var prop in entities) {
    if (entities.hasOwnProperty(prop)) {
      input = input.replace(new RegExp(prop, "g"), entities[prop]);
    }
  }
  return input;
}


A robust HTML entity encoder/decoder written in JavaScript.

https://mths.be/he

he (for “HTML entities”) is a robust HTML entity encoder/decoder written in JavaScript. It supports all standardized named character references as per HTML, handles ambiguous ampersands and other edge cases just like a browser would, has an extensive test suite, and — contrary to many other JavaScript solutions — he handles astral Unicode symbols just fine. An online demo is available.


Looks like this will do:

function html_entity_decode(s) {
  var t=document.createElement('textarea');
  t.innerHTML = s;
  var v = t.value;
  t.parentNode.removeChild(t);
  return v;
}

Source

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号