0

With the help of bucabay we are able to encode special characters into html entities below link for ref: (How to convert characters to HTML entities using plain JavaScript) Now we want to decode them i.e.how to convert HTML entities into special characters again.

Regards, AA.

Community
  • 1
  • 1
user2831401
  • 1
  • 1
  • 1
  • possible duplicate of [Convert HTML Character Entities back to regular text using javascript](http://stackoverflow.com/questions/4338963/convert-html-character-entities-back-to-regular-text-using-javascript) – Luca Borrione Apr 08 '14 at 14:44

3 Answers3

0

You can do it using the basic javascript or by using the jQuery..

newText = "Übergroße Äpfel mit Würmern";

var my_unescaped_text = jQuery('').html(newText).text();

Kiba
  • 10,155
  • 6
  • 27
  • 31
  • need to convert those html entities into special characters. – user2831401 Oct 07 '13 at 06:18
  • This introduces a security risk in the form of a cross-site scripting vulnerability: if the input (`newText`) contains something like ``, that `alert(1)` would be executed. This solution is also dependent on the browser’s support for character references, which even in modern browsers is not perfect across the board: http://mathias.html5.org/tests/html/ It also won’t work in non-browser environments. – Mathias Bynens May 23 '14 at 17:27
0

You can do this by making the browser parse the text as HTML, e.g.

var text = "Übergroße Äpfel mit Würmern";
var span = document.createElement('span');
span.innerHTML = text;
alert(span.innerHTML); // contains the characters as decoded
Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
  • This introduces a security risk in the form of a cross-site scripting vulnerability: if the input (`text`) contains something like ``, that `alert(1)` would be executed. This solution is also dependent on the browser’s support for character references, which even in modern browsers is not perfect across the board: http://mathias.html5.org/tests/html/ It also won’t work in non-browser environments. – Mathias Bynens May 23 '14 at 17:27
  • @MathiasBynens, there is nothing in the question that suggests that the data comes from an external source. Quite the opposite: the data is described as being created by encoding some characters as entities. – Jukka K. Korpela May 23 '14 at 19:04
0

For a robust solution that avoids the issues in the other answers, use the he library. From its README:

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.

Here’s how you’d use it:

var html = 'Übergroße Äpfel mit Würmern';
var decoded = he.decode(html);
// → `decoded` is now 'Übergroße Äpfel mit Würmern'

See this related Stack Overflow answer. And this one, too.

Community
  • 1
  • 1
Mathias Bynens
  • 144,855
  • 52
  • 216
  • 248