2

Is there some way to convert HTML Entities into unicode characters in Javascript?

I tried

var unicodeHtmlEntity = function (t) {
    numericValue = parseInt(t.slice(2, -1), 10);
    numericValue = numericValue.toString(16);
    return "\\u" + numericValue;
};

Where the function can be called like €, but returns the string \u20ac, not the unicode character for . Any ideas?

fcortes
  • 1,338
  • 3
  • 11
  • 26
  • I think this is the same with this thread: http://stackoverflow.com/questions/3835317/unicode-value-uxxxx-to-character-in-javascript Hope this helps! – Knx Feb 04 '14 at 08:34

1 Answers1

1

This should work for the vast majority of cases:

function unicodeHtmlEntity (t) {
    var numericValue = parseInt(t.slice(2, -1), 10);
    return String.fromCharCode(numericValue);
}

If you actually need to support characters in the "astral" planes, you can use: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint#Getting_it_to_work_with_higher_values in place of String.fromCharCode().

I presume you are aware that your code does not deal with hexadecimal entities, only decimal ones. To support those you could do something like:

function unicodeHtmlEntity (t) {
    var hex = t.charAt(2) === 'x';
    var base = hex ? 16 : 10;
    var pos = hex ? 3 : 2;
    var numericValue = parseInt(t.slice(pos, -1), base);
    return String.fromCharCode(numericValue);
}
Brett Zamir
  • 14,034
  • 6
  • 54
  • 77