[英]Uppercase and lowercase search
I am trying to make a search engine for my jsgrid, but I ran into a problem.我正在尝试为我的 jsgrid 制作一个搜索引擎,但我遇到了一个问题。 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您可以使用 Intl.Collator 执行不区分大小写的比较
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参考资料: 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: 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.