Efficiently replace all accented characters in a string?

后端 未结 21 2563
别跟我提以往
别跟我提以往 2020-11-22 04:35

For a poor man\'s implementation of near-collation-correct sorting on the client side I need a JavaScript function that does efficient single character rep

21条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 05:22

    A simple and easy way:

    function remove-accents(p){
    c='áàãâäéèêëíìîïóòõôöúùûüçÁÀÃÂÄÉÈÊËÍÌÎÏÓÒÕÖÔÚÙÛÜÇ';s='aaaaaeeeeiiiiooooouuuucAAAAAEEEEIIIIOOOOOUUUUC';n='';for(i=0;i=0){n+=s.substr(c.search(p.substr(i,1)),1);} else{n+=p.substr(i,1);}} return n;
    }
    

    So do this:

    remove-accents("Thís ís ân accêntéd phráse");
    

    Output:

    "This is an accented phrase"
    

提交回复
热议问题