Convert non-ASCII characters (umlauts, accents…) t

2019-01-15 05:18发布

I am looking for way in JavaScript to convert non-ASCII characters in a string to their closest equivalent, similarly to what the PHP iconv function does. For instance if the input string is Rånades på Skyttis i Ö-vik, it should be converted to Ranades pa skyttis i o-vik. I had a look at phpjs but iconv isn't included.

Is it possible to perform such conversion in JavaScript, if so how?

2条回答
太酷不给撩
2楼-- · 2019-01-15 06:08

The easiest way I've found:

var str = "Rånades på Skyttis i Ö-vik";
var combining = /[\u0300-\u036F]/g; 

console.log(str.normalize('NFKD').replace(combining, ''));
查看更多
倾城 Initia
3楼-- · 2019-01-15 06:11

It's because iconv is a native compiled UNIX utility behind the most i18n character map conversion functions.

You won't find it in javascript unless you access some browser component.

Encoding is a property of the document so most javascript implementation just simply dismiss it.

You'll need a pure js library for unaccented strings. It would be the best to have one for the specific language you need.

The simpliest way is via some translate tables or even regex replaces.

like here : http://lehelk.com/2011/05/06/script-to-remove-diacritics/

check this thread too : Replacing diacritics in Javascript

查看更多
登录 后发表回答