Convert Javascript UTF-8 to ASCII (e.g. Iconv ('UTF-8', 'ASCII // TRANSLIT', $ string) to PHP)

I am wondering how you can "translate" characters in UTF-8 to the nearest ASCII equivalent using Javascript, just like Iconv doest in PHP.

Example:

ΓΌ becomes u
- becomes o

I would prefer not to use a replacement, because a) it requires a full set of characters, which is a lot, and b) it would be difficult for me to get a full set of characters, and I will never be sure if I miss one or two.

+8
javascript utf-8 ascii iconv
source share
2 answers

As @Pointy said, your only option is to display / replace characters according to the dictionary.

You will find this really useful: https://github.com/backbone-paginator/backbone.paginator/blob/a579796a30e583c4dfa09e0a86e4abd21e0b5b56/plugins/diacritic.js

+8
source share

The easiest way I've found:

var str = "ΓΌΓ³"; var combining = /[\u0300-\u036F]/g; console.log(str.normalize('NFKD').replace(combining, '')); 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize

+9
source share

All Articles