Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
611 views
in Technique[技术] by (71.8m points)

jquery - javascript function to remove 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;

}

which works great but I'm wondering where to get those regular expression number from e.g: [/[300-306]/g, 'A'] ...

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.

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

octal values of characters


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...