user2831401
user2831401

Reputation: 1

How to convert HTML entities into special characters using plain JavaScript

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.

Upvotes: 0

Views: 6526

Answers (3)

Mathias Bynens
Mathias Bynens

Reputation: 149744

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.

Upvotes: 0

Jukka K. Korpela
Jukka K. Korpela

Reputation: 201798

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

Upvotes: 0

Kiba
Kiba

Reputation: 10857

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

Upvotes: 0

Related Questions