将非 ASCII 字符(变音、重音...)转换为最接近的 ASCII 等效字符(slug 创建) [英] Convert non-ASCII characters (umlauts, accents...) to their closest ASCII equivalent (slug creation)
本文介绍了将非 ASCII 字符(变音、重音...)转换为最接近的 ASCII 等效字符(slug 创建)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在JavaScript
中寻找方法将字符串中的非ASCII
字符转换为最接近的等价物,类似于PHP
iconv
函数可以.例如,如果输入字符串是 Rånades på Skyttis i Ö-vik
,则应将其转换为 Rånades pa skyttis i o-vik
.我查看了 phpjs
但不包括 iconv
.
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.
是否可以在 JavaScript 中执行此类转换,如果可以,如何执行?
推荐答案
我找到的最简单的方法:
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, ''));
参考https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize
这篇关于将非 ASCII 字符(变音、重音...)转换为最接近的 ASCII 等效字符(slug 创建)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文