4

可能重複:
Programatic Accent Reduction in JavaScript (aka text normalization or unaccenting)如何搜索字符串忽略重音字符(例如A = A)

我試圖找到一個字符串忽略口音,所以在我的例子如果我搜索aviãoaviao我總是得到兩個結果。 這裏是一個開始起來......

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <script type="text/javascript" src="http://jquery.com/src/jquery-latest.js"></script> 
    <script type="text/javascript"> 
     $(function() { 
      $('input').keyup(function (event) { 
       if (jQuery.trim($('input').val()) == "") { 
        return; 
       } 
       var str = jQuery.trim($('input').val()).split(" "); 
       jQuery.each($('table > tbody > tr > td:last-child'), function() { 
        var colval = $(this).html().toLowerCase(); 
        jQuery.each(str, function() { 
         var term = this.toLowerCase(); 
         if (colval.indexOf(term) != -1) { 
          alert(colval); 
         } 
        }); 
       }); 
      }); 
     }); 
    </script> 
</head> 
<body> 
    <input type="text" id="q" /> 
    <div> 
     <table> 
      <thead> 
       <tr> 
        <th>Id</th> 
        <th>Name</th> 
       </tr> 
      </thead> 
      <tbody> 
       <tr> 
        <td>1</td> 
        <td>avião resume</td> 
       </tr> 
       <tr> 
        <td>2</td> 
        <td>résumé</td> 
       </tr> 
       <tr> 
        <td>3</td> 
        <td>resume</td> 
       </tr> 
       <tr> 
        <td>4</td> 
        <td>aviao résumé</td> 
       </tr> 
      </tbody> 
     </table> 
    </div> 
</body> 
</html> 

Sample

+0

我編輯了標題的自由,以使其更清晰,可隨時輥B.如果你不喜歡它,請確認。 – 2011-05-03 20:41:40

+0

@Martijn類型 - 很好找 - 但前提,和可能的答案,是微妙的不同(字符串比較與在一個巨大的文件中搜索) – 2011-05-03 20:43:01

+0

@Pekka但使用JS字符串比較;最終它是一樣的解決方案。 – 2011-05-03 20:43:36

回答

3

對於它的價值,在一個點上我已經建立Latin-1的Unicode代碼點的這種映射到它們的ASCII同行。

你會使用這個爲:

var effectiveCharCode = CODE_NO_DIACRITICS[actualCharCode]; 

因此,字符串比較方法,其中< 0(S1 < S2),0(S1 == S2)> 0(S1> S2)可能是這樣:

function compareIgnoringDiacritics(s1, s2) { 
    for(var i = 0; i < s1.length && i < s2.length; i++){ 
    var cmp; 
    if((cmp = ((CODE_NO_DIACRITICS[s1.charCodeAt(i)] - CODE_NO_DIACRITICS[s2.charCodeAt(i)]))) != 0){ 
     return cmp; 
    } 
    } 
    return s1.length - s2.length; 
} 

這裏的表:

var CODE_NO_DIACRITICS = [ 

      0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 
      10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 
      20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 
      30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 
      40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 
      50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 
      60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 
      70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 
      80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 
      90, 91, 92, 93, 94, 95, 96, 65, 66, 67, 
      68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 
      78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 
      88, 89, 90, 123, 124, 125, 126, 127, 128, 129, 
      130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 
      140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 
      150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 
      160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 
      170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 
      180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 
      190, 191, 65, 65, 65, 65, 65, 65, 198, 67, 
      69, 69, 69, 69, 73, 73, 73, 73, 68, 78, 
      79, 79, 79, 79, 79, 215, 79, 85, 85, 85, 
      85, 89, 222, 223, 65, 65, 65, 65, 65, 65, 
      230, 67, 69, 69, 69, 69, 73, 73, 73, 73, 
      240, 78, 79, 79, 79, 79, 79, 247, 79, 85, 
      85, 85, 85, 89, 254, 89 
    ]; 
+0

原諒我的天真,但這將如何幫助我?(CODE_NO_DIACRITICS ['á'];返回undefined) – 2011-05-03 21:03:08

+0

那麼,該映射使用字符代碼不是字符(這個例子可能是誤導),所以你可能會使用字符串.charCodeAt()獲取字符串中每個字符的字符代碼值,然後映射該字符。與CODE_NO_DIACRITICS [someString.charCodeAt(0)]類似。 – BWW 2011-05-03 21:21:41