JS - jQuery的inarray IGNORECASE()和contains() [英] JS - jQuery inarray ignoreCase() and contains()

查看:171
本文介绍了JS - jQuery的inarray IGNORECASE()和contains()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很好,我更多的是PHP的人,而我的JS技能是接近无当涉及到比简单的设计相关业务以外的JS,所以原谅我,如果我要求明显。

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 - 但我不熟悉的语法这里战斗...)

the following operations would be a breeze in PHP (and might also be in JS - but I am fighting with unfamiliar syntax here ...)

有某种输入验证

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

这问题有2个部分。


  • 1 - ?我怎样才能使人们有可能为阵为区分大小写

例如。 - BRS201103-0783-CT-S 将产生相同的结果为 brs201103-0783-CT-S Brs201103-0783-CT-S 魔魔

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

基本上我需要像IGNORECASE()数组,但我无法找到jQuery来,任何引用,也没有JS ...

basically i need something like ignoreCase() for array , but I could not find any reference to that in jQuery nor JS...

我试过与toLowerCase() - 但它不工作(ittirating?)阵列上,也,这将解决混合大小写?

I tried toLowerCase() - but It is not working on the array (ittirating??) and also, would it resolve the mixed case ?


  • 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" )

推荐答案

您可以处理你的阵列是完全小写字母,小写和您的输入所以的indexOf()会像它的执行不区分大小写的搜索。

Part 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.

您可以用小写字符串与toLowerCase()因为你已经想通了。

You can lowercase a string with toLowerCase() as you've already figured out.

要做到一个阵列,可以使用...

To do an array, you can use...

arr = arr.map(function(elem) { return elem.toLowerCase(); }); 

第2部分

您可以检查一个字符串,例如...

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

另外,您也可以在这种情况下跳过改造阵列全部小写通过调用与toLowerCase()每个 ELEM 您检查之前的indexOf()

部分() 图() 没有老年IE的支持,但都是微不足道的填充工具。可在链接的文档,每个填充工具的一个例子。

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.

由于法布里西奥磨砂还指出,你可以在这里使用jQuery的等价物, $ .MAP() Array.prototype.map() $。gr​​ep的()长度属性 Array.prototype.some()。然后,您将获得浏览器的兼容性是免费的。

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.

这篇关于JS - jQuery的inarray IGNORECASE()和contains()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆