Efficiently replace all accented characters in a string?

后端 未结 21 2596
别跟我提以往
别跟我提以往 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:30

    Simply should be normalized chain and run a replacement codes:

    var str = "Letras Á É Í Ó Ú Ñ - á é í ó ú ñ...";
    console.log (str.normalize ("NFKD").replace (/[\u0300-\u036F]/g, ""));
    // Letras A E I O U N - a e i o u n...
    

    See normalize

    Then you can use this function:

    function noTilde (s) {
        if (s.normalize != undefined) {
            s = s.normalize ("NFKD");
        }
        return s.replace (/[\u0300-\u036F]/g, "");
    }
    

提交回复
热议问题