1

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?

3 Answers 3

3

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.)

Sign up to request clarification or add additional context in comments.

Comments

2

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

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

1 Comment

Note that this will only work in browsers, not in non-browser JavaScript environments such as Node.js. Also, it returns incorrect results in browsers that have buggy implementations of HTML entities. To ensure the results are correct, use a library.
-3

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

1 Comment

How does encoding URI components help decoding HTML entities?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.