javascript JS - jQuery 数组 ignoreCase() 和 contains()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11496914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JS - jQuery inarray ignoreCase() and contains()
提问by Obmerk Kronen
well, I am more of a PHP person, and my JS skills are close to none when it comes to any JS other than simple design related operations , so excuse me if I am asking the obvious .
好吧,我更像是一个 PHP 人,除了简单的设计相关操作之外,我的 JS 技能在任何 JS 方面几乎没有,所以如果我问的是显而易见的 .
the following operations would be a breeze in PHP (and might also be in JS - but I am fighting with unfamiliar syntax here ...)
以下操作在 PHP 中将是轻而易举的(也可能在 JS 中 - 但我在这里与不熟悉的语法作斗争......)
It is some sort of input validation
这是某种输入验证
var ar = ["BRS201103-0783-CT-S", "MAGIC WORD", "magic", "Words", "Magic-Word"];
jQuery(document).ready(function() {
jQuery("form#searchreport").submit(function() {
if (jQuery.inArray(jQuery("input:first").val(), ar) != -1){
jQuery("#contentresults").delay(800).show("slow");
return false;
}
This question has 2 parts .
这个问题有两部分。
- 1 - how can I make it possible for the array to be case insensitive ?
- 1 - 我怎样才能让数组不区分大小写?
E.g. - BRS201103-0783-CT-S
will give the same result as brs201103-0783-ct-s
AND Brs201103-0783-CT-s
or MAGIC magic Magic MaGIc
例如 -BRS201103-0783-CT-S
将给出与brs201103-0783-ct-s
AND相同的结果Brs201103-0783-CT-s
或MAGIC magic Magic MaGIc
basically i need something like ignoreCase() for array , but I could not find any reference to that in jQuery nor JS...
基本上我需要像 ignoreCase() 这样的数组,但是我在 jQuery 和 JS 中都找不到任何对它的引用......
I tried toLowerCase()
- but It is not working on the array (ittirating??) and also, would it resolve the mixed case ?
我试过toLowerCase()
- 但它不能在阵列上工作(令人兴奋??)而且,它会解决混合情况吗?
- 2 - How can I make the function to recognize only parts or combinations of the elements ?
- 2 - 我怎样才能让函数只识别元素的部分或组合?
E.g. - if one types only "word"
, I would like it to pass as "words"
, and also if someone types "some word"
it should pass (containing "word" )
例如 - 如果只有一个类型"word"
,我希望它通过 as "words"
,如果有人输入"some word"
它应该通过(包含“单词”)
回答by alex
Part 1
第1部分
You can process your array to be entirely lowercase, and lowercase your input so indexOf()
will work like it's performing a case insensitive search.
您可以将数组处理为完全小写,并将输入小写,这样indexOf()
就可以像执行不区分大小写的搜索一样工作。
You can lowercase a string with toLowerCase()
as you've already figured out.
toLowerCase()
正如您已经知道的那样,您可以将字符串小写。
To do an array, you can use...
要创建数组,您可以使用...
arr = arr.map(function(elem) { return elem.toLowerCase(); });
Part 2
第2部分
You could check for a substring, for example...
您可以检查子字符串,例如...
// Assuming you've already transformed the input and array to lowercase.
var input = "word";
var words = ["word", "words", "wordly", "not"];
var found = words.some(function(elem) { return elem.indexOf(input) != -1; });
Alternatively, you could skip in this instance transforming the array to be all lowercase by calling toLowerCase()
on each elem
before you check indexOf()
.
或者,您可以在此实例中通过在检查之前调用toLowerCase()
each来跳过将数组转换为全部小写。elem
indexOf()
some()
and map()
aren't supported in older IEs, but are trivial to polyfill. An example of a polyfill for each is available at the linked documentation.
some()
并且map()
在较旧的 IE 中不受支持,但对于 polyfill 来说是微不足道的。链接文档中提供了每个 polyfill 的示例。
As Fabrício Mattéalso pointed out, you can use the jQuery equivalents here, $.map()
for Array.prototype.map()
and $.grep()
with length
property for Array.prototype.some()
. Then you will get the browser compatibility for free.
正如Fabrício Matté还指出的,您可以在此处使用 jQuery 等价物,$.map()
forArray.prototype.map()
和$.grep()
withlength
属性 for Array.prototype.some()
。然后您将免费获得浏览器兼容性。
回答by Jose Manuel Gomez Alvarez
To check if an array contains an element, case-insensitive, I used this code:
要检查数组是否包含不区分大小写的元素,我使用了以下代码:
ret = $.grep( array, function (n,i) {
return ( n && n.toLowerCase().indexOf(elem.toLowerCase())!=-1 );
}) ;
Here is a fiddle to play with array match case insensitive
这是一个玩数组匹配不区分大小写的小提琴