Andres SK
Andres SK

Reputation: 10974

JavaScript character normalization

I'm using Google Translate API, and if I try to translate Mc Donald's this is what I get as a result:

Mc Donald's

How can I translate ' to ' -- in JavaScript -- and so on for any other numeric character reference? Thanks!

Upvotes: 0

Views: 730

Answers (3)

user168290
user168290

Reputation: 91

JS libraries often have helper api's for this, Prototype for example has its unescapeHTML() function on String that works perfect, notice the examples:

http://prototypejs.org/api/string/unescapeHTML

Upvotes: 1

kangax
kangax

Reputation: 39168

Shouldn't something like this do it?

'Mc Donald's'.replace(/&#(\d+);/g, function(m, g) {
  return String.fromCharCode(g);
});

Upvotes: 0

Related Questions