如何从字符串中删除重音字符? 特别是在IE6中,我有这样的东西:

accentsTidy = function(s){
    var r=s.toLowerCase();
    r = r.replace(new RegExp(/\s/g),"");
    r = r.replace(new RegExp(/[àáâãäå]/g),"a");
    r = r.replace(new RegExp(/æ/g),"ae");
    r = r.replace(new RegExp(/ç/g),"c");
    r = r.replace(new RegExp(/[èéêë]/g),"e");
    r = r.replace(new RegExp(/[ìíîï]/g),"i");
    r = r.replace(new RegExp(/ñ/g),"n");                
    r = r.replace(new RegExp(/[òóôõö]/g),"o");
    r = r.replace(new RegExp(/œ/g),"oe");
    r = r.replace(new RegExp(/[ùúûü]/g),"u");
    r = r.replace(new RegExp(/[ýÿ]/g),"y");
    r = r.replace(new RegExp(/\W/g),"");
    return r;
};

但是IE6让我很烦,它好像不喜欢我的正则表达式。


当前回答

将一个用户定义的函数传递给Array.sort()方法,并在这个用户定义的函数中使用String.localeCompare()

function myCompareFunction(a, b) {
  return a.localeCompare(b);
}

var values = ["pêches", "épinards", "tomates", "fraises"];

// WRONG: ["fraises", "pêches", "tomates", "épinards"]
values.sort();

 // **GOOD**: ["épinards", "fraises", "pêches", "tomates"]
values.sort(myCompareFunction);

其他回答

我使用GitHub Jakub Dundalek的存储库Latinize.js。

var latinize = require('latinize');
latinize('ỆᶍǍᶆṔƚÉ áéíóúýčďěňřšťžů'); // => 'ExAmPlE aeiouycdenrstzu'

假设你知道你在做什么,我怀疑IE6没有正确地解释文件的编码,因此不能识别文件中的非ascii字符:

确保文件保存为UTF-8格式(例如) 使用Fiddler或其他工具检查web服务器是否发送正确的内容编码HTTP报头。

(虽然它“闻起来”不对,但我会考虑做排序,比如在服务器上使用一些locale感知的东西……但无论如何…)

通过给定的测试,一个解决方案似乎要快得多:http://jsperf.com/diacritics/9

function removeDiacritics(str) {
   return str.replace(/[^A-Za-z0-9\s]+/g, function(a){
      return diacriticsMap[a] || a; 
   });
}
removeDiacritics(teste);

工作示例:http://jsbin.com/sovorute/1/edit

推理:这样做速度更快的一个原因是,我们只遍历由负正则表达式模式选择的特殊字符。最快的测试(不带in的字符串迭代)在给定文本上迭代1001,这意味着每个字符。这个函数只迭代了35次,输出了相同的结果。请记住,这将只替换地图中所指示的内容。

关于这个主题的经典文章:http://alistapart.com/article/accent-folding-for-auto-complete

来源:http://semplicewebsites.com/removing-accents-javascript,也提供了一个不错的人物地图。

从te reo Māori语言中移除宏子 给那些来这里想从马āori语中去掉宏子的人。 注意:当我格式化代码时,Stackblitz删除了map键上的单引号-他们必须确信这是OK的。这个代码的子集,谢谢

'use strict'; var macronMap = { ā: 'a', ē: 'e', ī: 'i', ō: 'o', ū: 'u', Ā: 'A', Ē: 'E', Ī: 'I', Ō: 'O', Ū: 'U', }; function deMacron(str) { return str.replace(/[^A-Za-z0-9\[\] ]/g, function (char) { return macronMap[char] || char; }); } function macronFree(str) { return str === deMacron(str); } console.assert( 'a, e, i, o, u, A, E, I, O, U' === deMacron('ā, ē, ī, ō, ū, Ā, Ē, Ī, Ō, Ū') ); console.assert(macronFree('a, e, i, o, u, A, E, I, O, U')); console.assert(!macronFree('ā, ē, ī, ō, ū, Ā, Ē, Ī, Ō, Ū')); document.body.append(deMacron('ā, ē, ī, ō, ū, Ā, Ē, Ī, Ō, Ū'));

谢谢大家 我使用这个版本并说明原因(因为我在开始时错过了这些解释,所以我试图帮助下一个读者,如果他和我一样无聊…)

备注:我想要一个高效的解决方案,所以:

只有一次regexp编译(如果需要) 每个字符串只扫描一个字符串 查找已翻译字符的有效方法 等等……

我的版本是: (里面没有新的技术技巧,只有一些精选的+解释)

makeSortString = (function() {
    var translate_re = /[¹²³áàâãäåaaaÀÁÂÃÄÅAAAÆccç©CCÇÐÐèéê?ëeeeeeÈÊË?EEEEE€gGiìíîïìiiiÌÍÎÏ?ÌIIIlLnnñNNÑòóôõöoooøÒÓÔÕÖOOOØŒr®Ršs?ߊS?ùúûüuuuuÙÚÛÜUUUUýÿÝŸžzzŽZZ]/g;
    var translate = {
"¹":"1","²":"2","³":"3","á":"a","à":"a","â":"a","ã":"a","ä":"a","å":"a","a":"a","a":"a","a":"a","À":"a","Á":"a","Â":"a","Ã":"a","Ä":"a","Å":"a","A":"a","A":"a",
"A":"a","Æ":"a","c":"c","c":"c","ç":"c","©":"c","C":"c","C":"c","Ç":"c","Ð":"d","Ð":"d","è":"e","é":"e","ê":"e","?":"e","ë":"e","e":"e","e":"e","e":"e","e":"e",
"e":"e","È":"e","Ê":"e","Ë":"e","?":"e","E":"e","E":"e","E":"e","E":"e","E":"e","€":"e","g":"g","G":"g","i":"i","ì":"i","í":"i","î":"i","ï":"i","ì":"i","i":"i",
"i":"i","i":"i","Ì":"i","Í":"i","Î":"i","Ï":"i","?":"i","Ì":"i","I":"i","I":"i","I":"i","l":"l","L":"l","n":"n","n":"n","ñ":"n","N":"n","N":"n","Ñ":"n","ò":"o",
"ó":"o","ô":"o","õ":"o","ö":"o","o":"o","o":"o","o":"o","ø":"o","Ò":"o","Ó":"o","Ô":"o","Õ":"o","Ö":"o","O":"o","O":"o","O":"o","Ø":"o","Œ":"o","r":"r","®":"r",
"R":"r","š":"s","s":"s","?":"s","ß":"s","Š":"s","S":"s","?":"s","ù":"u","ú":"u","û":"u","ü":"u","u":"u","u":"u","u":"u","u":"u","Ù":"u","Ú":"u","Û":"u","Ü":"u",
"U":"u","U":"u","U":"u","U":"u","ý":"y","ÿ":"y","Ý":"y","Ÿ":"y","ž":"z","z":"z","z":"z","Ž":"z","Z":"z","Z":"z"
    };
    return function(s) {
        return(s.replace(translate_re, function(match){return translate[match];}) );
    }
})();

我是这样用的:

var without_accents = makeSortString("wïthêüÄTrèsBïgüeAk100t");
// I let you guess the result,
// no I was kidding you : I give you the result : witheuatresbigueak100t

评论:

Tthe instruction inside it is done once (after, makeSortString != undefined) function(){...} is stored once in makeSortString, so the "big" translate_re and translate objects are stored once When you call makeSortString('something') it call directly the inside function which calls only s.replace(...) : it is efficient s.replace uses regexp (the special syntax of var translate_re= .... is in fact equivalent to var translate_re = new RegExp("[¹....Z]","g"); but the compilation of the regexp is done once for all, and the scan of the s String is done one for a call of the function (not for every character as it would be in a loop) For each character found s.replace calls function(match) where parameter match contains the character found, and it call the corresponding translated character (translate[match]) Translate[match] is probably efficient too as the javascript translate object is probably implemented by javascript with a hashtab or something equivalent and allow the program to find the translated character almost directly and not for instance through a loop on a array of all characters to find the right one (which would be awfully unefficient).