簡體   English   中英

大寫和小寫搜索

[英]Uppercase and lowercase search

我正在嘗試為我的 jsgrid 制作一個搜索引擎,但我遇到了一個問題。 如何搜索大寫和小寫文本?

因為現在,如果我使用相同的符號,我只會得到結果。

loadData: function (filter) {
  var ajaxItemsFiltered = [];
  $.each(ajaxItems, function (index, value) {
    var allow = true;
    $.each(value, function (index2, value2) {

      if (typeof filter[index2] !== "undefined" && filter[index2] !== '') {

        if (value2.indexOf(filter[index2]) === -1) {

          allow = false;
        }
      }
    });
    if (allow) {
      ajaxItemsFiltered.push(value);
    }
  });


  return ajaxItemsFiltered;
},

您可以使用 Intl.Collat​​or 執行不區分大小寫的比較

 const localeCompare = Intl.Collator('en', { sensitivity: 'base' }).compare; function includesByIgnoreCase(predicate) { if (this.filter(predicate).length > 0) { return true; } return false; }; const value2 = ['hElLo', 'world', 'foo', 'Hello Wolrd']; console.log( includesByIgnoreCase.call( value2, e => localeCompare(e, 'hello world') !== 0 ) );

參考資料: MDN: Intl.Collat​​or.prototype.compare

Javascript 是一種區分大小寫的語言,因此您需要將兩個比較值更改為“大寫”或“小寫”以進行檢查,而不必擔心區分大小寫,例如:

if (value2.toLowerCase().indexOf(filter[index2].toLowerCase()) === -1)
//OR
if (value2.toUpperCase().indexOf(filter[index2].toUpperCase()) === -1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM