最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Convert accented text into ASCII characters? - Stack Overflow

programmeradmin18浏览0评论

I would like to convert accented letters and various encodings into the plain English ASCII one in Javascript and wonder what are the possible options. What I need is that:

éclair ~bees~ eclair

bär ~bees~ bar

привет ~bees~ privet

こんにちは ~bees~ konnichiva

As you can see the idea is that any language gets converted into the plain English ASCII equivalent. The áčçéñtèd letters are converted into their plain equivalents, letters in cyrillic or japanese encoding are converted into into their transliterated equivalent.

Anyone knows an approach to do that in Javascript?

I would like to convert accented letters and various encodings into the plain English ASCII one in Javascript and wonder what are the possible options. What I need is that:

éclair ~bees~ eclair

bär ~bees~ bar

привет ~bees~ privet

こんにちは ~bees~ konnichiva

As you can see the idea is that any language gets converted into the plain English ASCII equivalent. The áčçéñtèd letters are converted into their plain equivalents, letters in cyrillic or japanese encoding are converted into into their transliterated equivalent.

Anyone knows an approach to do that in Javascript?

Share Improve this question edited Apr 28, 2016 at 5:59 ciekawy 2,3371 gold badge25 silver badges41 bronze badges asked Mar 18, 2014 at 0:33 AerodynamikaAerodynamika 8,41318 gold badges90 silver badges148 bronze badges 1
  • related: stackoverflow./questions/990904/… – Bergi Commented Nov 21, 2022 at 20:15
Add a ment  | 

3 Answers 3

Reset to default 33

There are a number of Node modules that do similar things but are much lighter-weight than node-iconv, and in particular, are in all JS and don't require you to pile any C or C++:

  1. node-unidecode appears to do mostly what you asked for:

    $ npm install unidecode
    ...
    [email protected] node_modules/unidecode
    $ node
    > var unidecode = require('unidecode');
    undefined
    > unidecode('éclair')
    'eclair'
    > unidecode('bär')
    'bar'
    > unidecode('привет')
    'priviet'
    > unidecode('こんにちは')
    'konnitiha'
    
  2. node-transliterator is even lighter-weight, but behaves even further from what you asked for:

    $ npm install transliterator
    ...
    [email protected] node_modules/transliterator
    $ node
    > var transliterator = require('transliterator');
    undefined
    > transliterator('éclair')
    'eclair'
    > transliterator('bär')
    'baer'
    > transliterator('привет')
    ''
    > transliterator('こんにちは')
    ''
    
  3. node-urlify is slightly closer but also way further from what you asked for:

    $ npm install urlify
    ...
    [email protected] node_modules/urlify
    $ node
    > var urlify = require('urlify').create({ spaces: ' ' });
    undefined
    > urlify('éclair')
    'eclair'
    > urlify('bär')
    'bar'
    > urlify('привет')
    'privet'
    > urlify('こんにちは')
    '_____'
    
  4. Finally, limax is more heavyweight, when I did npm install limax it printed lots of C piler warnings, but it still just worked and is the closest to what you asked for:

    $ npm install limax
    ...
    [email protected] node_modules/limax
    ├── [email protected]
    ├── [email protected]
    ├── [email protected] ([email protected])
    └── [email protected]
    $ node
    > var slug = require('limax')
    undefined
    > slug('éclair')
    'eclair'
    > slug('bär')
    'baer'
    > slug('привет')
    'privet'
    > slug('こんにちは')
    'konnichiha'
    

Checkout node-iconv.

Looks like node equivalent of php iconv.

Author here. You may try the transliteration module. It can be run in both browser and node.js and with no dependencies

发布评论

评论列表(0)

  1. 暂无评论