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?
The easiest way I've found:
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