JavaScript case insensitive string comparison

2018-12-31 04:43发布

How do I perform case insensitive string comparison in JavaScript?

18条回答
听够珍惜
2楼-- · 2018-12-31 05:37

This is an improved version of this answer.

String.equal = function (s1, s2, ignoreCase, useLocale) {
    if (s1 == null || s2 == null)
        return false;

    if (!ignoreCase) {
        if (s1.length !== s2.length)
            return false;

        return s1 === s2;
    }

    if (useLocale) {
        if (useLocale.length)
            return s1.toLocaleLowerCase(useLocale) === s2.toLocaleLowerCase(useLocale)
        else
            return s1.toLocaleLowerCase() === s2.toLocaleLowerCase()
    }
    else {
        if (s1.length !== s2.length)
            return false;

        return s1.toLowerCase() === s2.toLowerCase();
    }
}



Usages & tests:

String.equal = function (s1, s2, ignoreCase, useLocale) {
    if (s1 == null || s2 == null)
        return false;

    if (!ignoreCase) {
        if (s1.length !== s2.length)
            return false;

        return s1 === s2;
    }

    if (useLocale) {
        if (useLocale.length)
            return s1.toLocaleLowerCase(useLocale) === s2.toLocaleLowerCase(useLocale)
        else
            return s1.toLocaleLowerCase() === s2.toLocaleLowerCase()
    }
    else {
        if (s1.length !== s2.length)
            return false;

        return s1.toLowerCase() === s2.toLowerCase();
    }
}

// If you don't mind extending the prototype.
String.prototype.equal = function(string2, ignoreCase, useLocale) {
  return String.equal(this.valueOf(), string2, ignoreCase, useLocale);
}

// ------------------ TESTS ----------------------
console.log("Tests...");

console.log('Case sensitive 1');
var result = "Abc123".equal("Abc123");
console.assert(result === true);

console.log('Case sensitive 2');
result = "aBC123".equal("Abc123");
console.assert(result === false);

console.log('Ignore case');
result = "AbC123".equal("aBc123", true);
console.assert(result === true);

console.log('Ignore case + Current locale');
result = "AbC123".equal("aBc123", true);
console.assert(result === true);

console.log('Turkish test 1 (ignore case, en-US)');
result = "IiiI".equal("ıiİI", true, "en-US");
console.assert(result === false);

console.log('Turkish test 2 (ignore case, tr-TR)');
result = "IiiI".equal("ıiİI", true, "tr-TR");
console.assert(result === true);

console.log('Turkish test 3 (case sensitive, tr-TR)');
result = "IiiI".equal("ıiİI", false, "tr-TR");
console.assert(result === false);

console.log('null-test-1');
result = "AAA".equal(null);
console.assert(result === false);

console.log('null-test-2');
result = String.equal(null, "BBB");
console.assert(result === false);

console.log('null-test-3');
result = String.equal(null, null);
console.assert(result === false);

查看更多
唯独是你
3楼-- · 2018-12-31 05:39

Even this question have already answered. I have a different approach to use RegExp and match to ignore case sensitive. Please see my link https://jsfiddle.net/marchdave/7v8bd7dq/27/

$("#btnGuess").click(guessWord);

  function guessWord() {

   var letter = $("#guessLetter").val();
   var word = 'ABC';
   var pattern = RegExp(letter, 'gi'); // pattern: /a/gi

   var result = word.match(pattern);
   alert('Ignore case sensitive:' + result);

  }
查看更多
看淡一切
4楼-- · 2018-12-31 05:41

The simplest way to do it (if you're not worried about special Unicode characters) is to call toUpperCase:

var areEqual = string1.toUpperCase() === string2.toUpperCase();
查看更多
裙下三千臣
5楼-- · 2018-12-31 05:43

I have recently created a micro library that provides case-insensitive string helpers: https://github.com/nickuraltsev/ignore-case. (It uses toUpperCase internally.)

var ignoreCase = require('ignore-case');

ignoreCase.equals('FOO', 'Foo'); // => true
ignoreCase.startsWith('foobar', 'FOO'); // => true
ignoreCase.endsWith('foobar', 'BaR'); // => true
ignoreCase.includes('AbCd', 'c'); // => true
ignoreCase.indexOf('AbCd', 'c'); // => 2
查看更多
浮光初槿花落
6楼-- · 2018-12-31 05:43

I wrote a extension. very trivial

if (typeof String.prototype.isEqual!= 'function') {
    String.prototype.isEqual = function (str){
        return this.toUpperCase()==str.toUpperCase();
     };
}
查看更多
与君花间醉酒
7楼-- · 2018-12-31 05:44
str = 'Lol', str2 = 'lOl', regex = new RegExp('^' + str + '$', 'i');
if (regex.test(str)) {
    console.log("true");
}
查看更多
登录 后发表回答