Decode HTML entities in JavaScript? [duplicate]
Asked Answered
S

3

10

Sample conversions:

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

Any small library function that can handle this?

Saharanpur answered 11/5, 2010 at 5:55 Comment(0)
L
29

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;
}
Lenzi answered 11/5, 2010 at 6:1 Comment(1)
please see my comment to bboeSaharanpur
T
4

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.

Trocar answered 7/8, 2017 at 9:59 Comment(1)
the last update to this was in 2018, so I would avoid this library if possibleKoontz
A
3

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

Astigmia answered 11/5, 2010 at 6:2 Comment(2)
I have no document object and this should not rely on it, since I'm using JSM XUL.Saharanpur
You can use the code (phpjs.org/functions/htmlentities:425) which relies on a table lookup. You need to reverse the lookup to decode the entities.Astigmia

© 2022 - 2024 — McMap. All rights reserved.