Efficiently replace all accented characters in a string?

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

    I made a Prototype Version of this:

    String.prototype.strip = function() {
      var translate_re = /[öäüÖÄÜß ]/g;
      var translate = {
        "ä":"a", "ö":"o", "ü":"u",
        "Ä":"A", "Ö":"O", "Ü":"U",
        " ":"_", "ß":"ss"   // probably more to come
      };
        return (this.replace(translate_re, function(match){
            return translate[match];})
        );
    };
    

    Use like:

    var teststring = 'ä ö ü Ä Ö Ü ß';
    teststring.strip();
    

    This will will change the String to a_o_u_A_O_U_ss

提交回复
热议问题