javascript函数删除变音符号 [英] javascript function to remove diacritics
问题描述
我正在搜索javascript函数来替换法语diacritics并且来自这段代码:
I'm searching for javascript function to replace French diacritics and came accross this code:
String.prototype.removeDiacritics = function() {
var diacritics = [
[/[\300-\306]/g, 'A'],
[/[\340-\346]/g, 'a'],
[/[\310-\313]/g, 'E'],
[/[\350-\353]/g, 'e'],
[/[\314-\317]/g, 'I'],
[/[\354-\357]/g, 'i'],
[/[\322-\330]/g, 'O'],
[/[\362-\370]/g, 'o'],
[/[\331-\334]/g, 'U'],
[/[\371-\374]/g, 'u'],
[/[\321]/g, 'N'],
[/[\361]/g, 'n'],
[/[\307]/g, 'C'],
[/[\347]/g, 'c'],
];
var s = this;
for (var i = 0; i < diacritics.length; i++) {
s = s.replace(diacritics[i][0], diacritics[i][1]);
}
return s;
}
哪作得好但我我想知道从哪里得到那些正则表达式数字:例如:
[/ [\ - 300-\ 306] / g,'A'] ......
which works great but I'm wondering where to get those regular expression number from e.g: [/[\300-\306]/g, 'A'] ...
我问的原因是因为我注意到替换列表缺少ÿ字符,但我不知道正则表达式将replace替换为y。
The reason i ask is because i've noticed that the replacing list is missing the ÿ character but I have no idea whats the regular expression to replace ÿ to y.
谢谢!
推荐答案
这三位数字是八进制的,所以如果你取un的un值并将其转换为八进制,这是377,你可以将它添加到列表中:
Those three digit numbers are in octal, so if you take the unicode value of ÿ and convert it to octal, which is 377, you will be able to add it to the list:
var diacritics = [
// Your other values...
[/[\377]/g, 'y'],
// ...
];
这是一个查找字符八进制值的好网站:
Here is a good site to look up the octal values of characters:
这篇关于javascript函数删除变音符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!