简体   繁体   中英

Uppercase and lowercase search

I am trying to make a search engine for my jsgrid, but I ran into a problem. How to search uppercase and lowercase text?

Because now, I only get results if I use the same symbols.

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;
},

You can use Intl.Collator to perform case insensitive comparisons

 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 ) );

References: MDN: Intl.Collator.prototype.compare

Javascript is a sensitive case language so you need to change both compared values to "Upper" or "Lower" case to make the check without worrying about the case sensitivity, like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM