Remove accents/diacritics in a string in JavaScript

前端 未结 29 2538
轻奢々
轻奢々 2020-11-21 13:29

How do I remove accentuated characters from a string? Especially in IE6, I had something like this:

accentsTidy = function(s){
    var r=s.toLowerCase();
           


        
29条回答
  •  长发绾君心
    2020-11-21 13:56

    The format for new RegExp is

    RegExp(something, 'modifiers');
    

    So you would want

    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;
                    };
    

提交回复
热议问题