Javascript javascript比较字符串而不区分大小写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4919403/
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
javascript compare strings without being case sensitive
提问by ankur
I have to check some strings using JavaScript but case sensitivity is causing problems. for example
我必须使用 JavaScript 检查一些字符串,但区分大小写会导致问题。例如
if('abc'=='ABC')
{
return true;
}
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 循环。我也不能使用 tolower 子句,因为我不知道数据它会如何来对 ex 意味着:
if('aBc'=='abC')
{
return true;
}
how to write the JS function for this if it could be done by jquery.
如果可以通过 jquery 完成,如何为此编写 JS 函数。
回答by Gazler
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
}
回答by Akrikos
Another method using a regular expression (this is more correct than Zachary's answer):
使用正则表达式的另一种方法(这比 Zachary 的答案更正确):
var string1 = 'someText',
string2 = 'SometexT',
regex = new RegExp('^' + string1 + '$', 'i');
if (regex.test(string2)) {
return true;
}
RegExp.test() will return true or false.
RegExp.test() 将返回 true 或 false。
Also, adding the '^' (signifying the start of the string) to the beginning and '$' (signifying the end of the string) to the end make sure that your regular expression will match only if 'sometext' is the only text in stringToTest. If you're looking for text that contains the regular expression, it's ok to leave those off.
此外,将 '^'(表示字符串的开头)添加到开头,将 '$'(表示字符串的结尾)添加到结尾,确保您的正则表达式仅在 'sometext' 是唯一的文本时匹配在 stringToTest 中。如果您正在查找包含正则表达式的文本,则可以将其关闭。
It might just be easier to use the string.toLowerCase() method.
使用 string.toLowerCase() 方法可能更容易。
So... regular expressions are powerful, but you should only use them if you understand how they work. Unexpected things can happen when you use something you don't understand.
所以……正则表达式很强大,但只有在了解它们的工作原理时才应该使用它们。当你使用你不理解的东西时,可能会发生意想不到的事情。
There are tons of regular expression 'tutorials', but most appear to be trying to push a certain product. Here's what looks like a decent tutorial... granted, it's written for using php, but otherwise, it appears to be a nice beginner's tutorial: http://weblogtoolscollection.com/regex/regex.php
有大量的正则表达式“教程”,但大多数似乎都在尝试推出某个产品。这是一个看起来不错的教程......当然,它是为使用 php 而编写的,但除此之外,它似乎是一个不错的初学者教程:http: //weblogtoolscollection.com/regex/regex.php
This appears to be a good tool to test regular expressions: http://gskinner.com/RegExr/
这似乎是测试正则表达式的好工具:http: //gskinner.com/RegExr/
回答by Zachary
You can also use string.match().
您也可以使用 string.match()。
var string1 = "aBc";
var match = string1.match(/AbC/i);
if(match) {
}
回答by belugabob
Try this...
尝试这个...
if(string1.toLowerCase() == string2.toLowerCase()){
return true;
}
Also, it's not a loop, it's a block of code. Loops are generally repeated (although they can possibly execute only once), whereas a block of code never repeats.
此外,它不是一个循环,它是一个代码块。循环通常会重复(尽管它们可能只执行一次),而代码块从不重复。
I read your note about not using toLowerCase, but can't see why it would be a problem.
我阅读了您关于不使用 toLowerCase 的说明,但不明白为什么会出现问题。