js判断文字列大文字小文字を無視javaのequalsIgnoreCaseに類似
770 ワード
//js大文字と小文字を無視する方法をテストする
String.prototype.equalsIgnoreCase = function(anotherString) {
if(this === anotherString){ // null
return true;
}
// typeof(null) = object typeof(undefined) = undefined
if(typeof(anotherString)==='string'){ //this!=null&&this!=undefined &&anotherString!=null&& anotherString!=undefined
return this.toLowerCase() == anotherString.toLowerCase(); //
}
return false;
}
//
console.log("1".equalsIgnoreCase("1")); // true
console.log("1".equalsIgnoreCase(1)); //false
console.log(" AAA".equalsIgnoreCase(" aaa")); // true
console.log(" AAA".equalsIgnoreCase(" AAA")); //true