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

2

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

var element = document.createElement('div');
element.innerHTML = toto;
console.log(element.textContent);
> "Lo�?¯c"
1
  • 1
    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. Commented Dec 9, 2013 at 13:56
-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
  • 2
    How does encoding URI components help decoding HTML entities?
    – Oriol
    Commented Apr 30, 2015 at 16:06

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.