1
มอเตอร์ไซค์

Can I convert this unicode to string with JS. (It is Thailand Language)

I use

console.log(String.fromCharCode("มอเตอร์ไซค์"));

And It's not correct. if it right it will show มอเตอร์ไซค์

2 Answers2

4

Your Unicode string is encoded using HTML entity notation. Generally that means that whatever encoded the string expected it to end up in the middle of an HTML document, where it would be seen by an HTML parser.

If you've somehow got that string in JavaScript in a browser, you can get to the encoded Unicode by letting the browser parse it:

var str = "มอเตอร์ไซค์";
var elem = document.createElement("div");
elem.innerHTML = str;
alert(elem.textContent);

The string.fromCharCode() function expects one or more numeric arguments; it won't understand HTML entities. Thus if you're not in a browser (like, if you've got the string in a Node.js program or something like that), you could convert the string with your own code:

var str = "มอเตอร์ไซค์";
var thai = String.fromCharCode.apply(String, str.match(/x[^;]*;/g).map(function(n) { return parseInt(n.slice(1, -1), 16); }));

That conversion will only work when the code points involved are within the first 64K values.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • Note that `.textContent` might not work everywhere; in IE you may have to use `.innerText` instead, at least in older versions. – Pointy Jul 17 '15 at 12:27
2

You may want something like this :

var input = "มอเตอร์ไซค์";

var output = input.replace(/&#x[0-9A-Fa-f]+;/g,
                           function(htmlCode) {
                               var codePoint = parseInt( htmlCode.slice(3, -1), 16 );
                               return String.fromCharCode( codePoint );
                           });
Mathias Dolidon
  • 3,775
  • 1
  • 20
  • 28