繁体   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