Efficiently replace all accented characters in a string?

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

    I've solved it another way, if you like.

    Here I used two arrays where searchChars containing which will be replaced and replaceChars containing desired characters.

    var text = "your input string";
    var searchChars = ['Å','Ä','å','Ö','ö']; // add more charecter.
    var replaceChars = ['A','A','a','O','o']; // exact same index to searchChars.
    var index;
    for (var i = 0; i < text.length; i++) {
      if( $.inArray(text[i], searchChars) >-1 ){ // $.inArray() is from jquery.
        index = searchChars.indexOf(text[i]);
        text = text.slice(0, i) + replaceChars[index] + text.slice(i+1,text.length);
      }
    }

提交回复
热议问题