wawanopoulos
wawanopoulos

Reputation: 9794

Decode HTML character references in JavaScript string

In my program, I have this string in entry:

var toto = "Loïc";

I would like to decode this String in order to have:

'Loïc'

How can I do that?

Upvotes: 1

Views: 2595

Answers (3)

Ardavan Dejpanah
Ardavan Dejpanah

Reputation: 106

Check out the built-in JavaScript function encodeURIComponent(str) and encodeURI(str).

var muUrl="http://www.google.com?ul=اردوان";
var myUrl2 = "http://XXXX.com/default.aspx?link=" + encodeURIComponent(myUrl);

In your case, this should work

Upvotes: -3

Mathias Bynens
Mathias Bynens

Reputation: 149484

These are HTML character references, also known as “HTML entities”.

To decode (or encode) them in JavaScript, it’s best to use the he library. 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.

var input = 'foo © bar ≠ baz 𝌆 qux';
var output = he.decode(input);
console.log(output);
// → 'foo © bar ≠ baz 𝌆 qux'

(All this has nothing to do with the UTF-8 encoding, by the way. I’ve removed the tag from this question.)

Upvotes: 3

Jonathan Naguin
Jonathan Naguin

Reputation: 14766

à and à are entities numbers. You can use this:

var element = document.createElement('div');
element.innerHTML = toto;
console.log(element.textContent);
> "Loïc"

Upvotes: 2

Related Questions