Max
Max

Reputation: 13334

Convert non-ASCII characters (umlauts, accents...) to their closest ASCII equivalent (for slug creation)

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?

Notes:

Upvotes: 43

Views: 17431

Answers (3)

Adam
Adam

Reputation: 29069

I would recommend Unicode package, it will also map Greek and Cyrillic letters to their closest ascii symbol:

unidecode('Lillı Celiné Никита Ödipus');

'Lilli Celine Nikita Odipus'

Upvotes: 5

Rez
Rez

Reputation: 720

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, ''));

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

Upvotes: 45

kisp
kisp

Reputation: 6552

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

Upvotes: 3

Related Questions