javascript比较字符串而不区分大小写 [英] javascript compare strings without being case sensitive

查看:2562
本文介绍了javascript比较字符串而不区分大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用JavaScript检查一些字符串,但区分大小写会导致问题。例如

I have to check some strings using JavaScript but case sensitivity is causing problems. for example

if('abc'=='ABC')
{
return true;
}

尽管单词的含义相同,但它不会进入if循环内部。
我也不能使用tolower条款,因为我不知道数据会怎样,这意味着
ex:

it will not go inside the if loop though the meaning of the word are same. I cant use tolower clause too since i dont know the data how it would come it means for ex:

if('aBc'=='abC')
{
return true;
}

如果可以通过jquery完成,可以为此编写JS函数。

how to write the JS function for this if it could be done by jquery.

推荐答案

你可以将两个参数都设置为小写,这样你总会得到一个不区分大小写的搜索。

You can make both arguments lower case, and that way you will always end up with a case insensitive search.

var string1 = "aBc";
var string2 = "AbC";

if (string1.toLowerCase() === string2.toLowerCase())
{
    #stuff
}

这篇关于javascript比较字符串而不区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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