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.

1

3 Answers 3

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();

2
  • need to convert those html entities into special characters. Oct 7, 2013 at 6:18
  • This introduces a security risk in the form of a cross-site scripting vulnerability: if the input (newText) contains something like <svg onload=alert(1)>, 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: mathias.html5.org/tests/html It also won’t work in non-browser environments. May 23, 2014 at 17:27
0

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

var text = "&Uuml;bergro&szlig;e &Auml;pfel mit W&uuml;rmern";
var span = document.createElement('span');
span.innerHTML = text;
alert(span.innerHTML); // contains the characters as decoded
2
  • This introduces a security risk in the form of a cross-site scripting vulnerability: if the input (text) contains something like <svg onload=alert(1)>, 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: mathias.html5.org/tests/html It also won’t work in non-browser environments. May 23, 2014 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. May 23, 2014 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 = '&Uuml;bergro&szlig;e &Auml;pfel mit W&uuml;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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.