6

How do I transliterate Unicode characters to ASCII in pure Javascript?

input:  'Ǐńťęř'
output: 'Inter'

I need similar effect as shells iconv -f UTF-8 -t 'ASCII//TRANSLIT' but in vanilla Javascript.

dda
  • 6,030
  • 2
  • 25
  • 34
Michał Šrajer
  • 30,364
  • 7
  • 62
  • 85
  • 3
    Possible duplicate of [Efficiently replace all accented characters in a string?](http://stackoverflow.com/questions/286921/efficiently-replace-all-accented-characters-in-a-string) – SpYk3HH Jan 14 '17 at 16:26
  • Your Question already has an answer, but this is just for fun: [Fun with Uni!!](https://jsfiddle.net/SpYk3/814000tj/show) – SpYk3HH Jan 14 '17 at 16:26
  • Not a duplicate. Transliterating to ASCII is more of a compression function than replacing some of the letters with European stuff on them. – William Entriken Aug 15 '18 at 20:49

1 Answers1

0

Use the answer from Efficiently replace all accented characters in a string? like this:

var makeSortString = (function() {
  var translate_re = /[αβγ]/g; // etc.
  var translate = {
    "α": "a", // alpha - a
    "β": "b", // beta -- b
    "γ": "g"  // gamma - g
    // etc.
  };
  return function(s) {
    return ( s.replace(translate_re, function(match) { 
      return translate[match]; 
    }) );
  }
})();

You could extend it to include only the characters you want.

merrybot
  • 155
  • 8