如何在JavaScript中执行不区分大小写的字符串比较?
当前回答
为了获得更好的浏览器兼容性,可以使用正则表达式。这将适用于过去20年发布的所有web浏览器:
String.prototype.equalsci = function(s) {
var regexp = RegExp("^"+this.replace(/[.\\+*?\[\^\]$(){}=!<>|:-]/g, "\\$&")+"$", "i");
return regexp.test(s);
}
"PERSON@Ü.EXAMPLE.COM".equalsci("person@ü.example.com")// returns true
这与这里找到的其他答案不同,因为它考虑到了并非所有用户都在使用现代网络浏览器。
注意:如果你需要支持不寻常的情况,如土耳其语,你将需要使用localeCompare,因为i和i在土耳其语中不是同一个字母。
"I".localeCompare("i", undefined, { sensitivity:"accent"})===0// returns true
"I".localeCompare("i", "tr", { sensitivity:"accent"})===0// returns false
其他回答
我写了一个扩展。很琐碎的
if (typeof String.prototype.isEqual!= 'function') {
String.prototype.isEqual = function (str){
return this.toUpperCase()==str.toUpperCase();
};
}
使用RegEx进行字符串匹配或比较。
在JavaScript中,你可以使用match()进行字符串比较, 别忘了把I放到正则表达式里。该标志将强制进行不区分大小写的测试。
例子:
为了确认任何情况下的字符串测试都包含在matchString变量内的任何位置
var matchString = "Test";
if (matchString.match(/test/i)) {
alert('matchString contains the substring "test" case insensitive');
}
else {
alert('matchString does not contain the substring "test" case insensitive');
}
要确认matchString变量只包含任何情况下的test,而不包含其他字符,则在正则表达式中使用零宽度断言^和$。这些将要求test分别直接出现在字符串的开始之后和字符串的结束之前。
var matchString = "Test";
if (matchString.match(/^test$/i)) {
alert('matchString equals "test" case insensitive');
}
else {
alert('matchString does not equal "test" case insensitive');
}
我们也可以使用ASCII来实现:
function toLower(a){
let c = "";
for(let i = 0;i<a.length;i++){
let f = a.charCodeAt(i);
if(f < 95){
c += String.fromCharCode(f+32);
}
else{
c += a[i];
}
}
return c;
}
function compareIt(a,b){
return toLower(a)==toLower(b);
}
console.log(compareIt("An ExamPlE" , "an example"));
这里有很多答案,但我喜欢添加一个基于扩展String库的解决方案:
String.prototype.equalIgnoreCase = function(str)
{
return (str != null
&& typeof str === 'string'
&& this.toUpperCase() === str.toUpperCase());
}
这样你就可以像在Java中那样使用它!
例子:
var a = "hello";
var b = "HeLLo";
var c = "world";
if (a.equalIgnoreCase(b)) {
document.write("a == b");
}
if (a.equalIgnoreCase(c)) {
document.write("a == c");
}
if (!b.equalIgnoreCase(c)) {
document.write("b != c");
}
输出将是:
"a == b"
"b != c"
String.prototype.equalIgnoreCase = function(str) { 返回(str != null && Typeof STR === 'string' && this.toUpperCase() === str.toUpperCase()); } Var a = "hello"; var b = "HeLLo"; Var c = "world"; if (a.equalIgnoreCase(b)) { 文档。写("a == b"); document . write(“< br > "); } if (a.equalIgnoreCase(c)) { 文档。写("a == c"); } if (!b.equalIgnoreCase(c)) { 文档。写("b != c"); }
假设我们想要在字符串变量大海捞针。这里有三个陷阱:
国际化的应用程序应该避免字符串。toUpperCase和string.toLowerCase。请使用忽略大小写的正则表达式。例如,var needleRegExp = new RegExp(needle, "i");然后是needleRegExp.test(haystack)。 一般来说,你可能不知道针的价值。注意针头不包含任何正则表达式特殊字符。使用needle.replace(/[-[\]{}()*+?, \ \ ^ $ | # \] / g , "\\$&");. 在其他情况下,如果您想精确匹配needle和haystack,只需忽略case,请确保在正则表达式构造函数的开头添加“^”,并在末尾添加“$”。
考虑到第(1)和(2)点,一个例子是:
var haystack = "A. BAIL. Of. Hay.";
var needle = "bail.";
var needleRegExp = new RegExp(needle.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), "i");
var result = needleRegExp.test(haystack);
if (result) {
// Your code here
}