Efficiently replace all accented characters in a string?

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

    If you want to achieve sorting where "ä" comes after "a" and is not treated as the same, then you can use a function like mine.

    You can always change the alphabet to get different or even weird sortings. However, if you want some letters to be equivalent, then you have to manipulate the strings like a = a.replace(/ä/, 'a') or similar, as many have already replied above. I've included the uppercase letters if someone wants to have all uppercase words before all lowercase words (then you have to ommit .toLowerCase()).

    function sortbyalphabet(a,b) {
            alphabet = "0123456789AaÀàÁáÂâÃãÄäBbCcÇçDdÈèÉéÊêËëFfGgHhÌìÍíÎîÏïJjKkLlMmNnÑñOoÒòÓóÔôÕõÖöPpQqRrSsTtÙùÚúÛûÜüVvWwXxÝýŸÿZz";
            a = a.toLowerCase();
            b = b.toLowerCase();
            shorterone = (a.length > b.length ? a : b);
            for (i=0; i

提交回复
热议问题