Remove accents/diacritics in a string in JavaScript

前端 未结 29 2577
轻奢々
轻奢々 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:46

    Pass a user defined function to the Array.sort() method, and in this user defined function use 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);
    

提交回复
热议问题