javascript tutorial - [Solved-5 Solutions] Javascript case insensitive string comparison - javascript - java script - javascript array



Problem:

How do we perform case insensitive string comparison in JavaScript?

Solution 1:

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();
click below button to copy the code. By JavaScript tutorial team

Solution 2:

  • The best way to do a case insensitive comparison in JavaScript is to use RegExp match() method with the 'i' flag.
  • JavaScript: case-insensitive search.
  • When both strings being compared are variables (not constants), then it's a little more complicated 'cause we need to generate a RegExp from the string but passing the string to RegExp constructor can result in incorrect matches or failed matches if the string has special regex characters in it.
  • If we care about internationalization don't use toLowerCase() or toUpperCase() as it doesn't provide accurate case-insensitive comparisons in all languages.

Solution 3:

Remember that casing is a locale specific operation. Depending on scenario we may want to take that in to account. For example, if we are comparing names of two people we may want to consider locale but if we are comparing machine generated values such as UUID then we might not. This why we use following function in my utils library (note that type checking is not included for performance reason).

function compareStrings (string1, string2, ignoreCase, useLocale) {
    if (ignoreCase) {
        if (useLocale) {
            string1 = string1.toLocaleLowerCase();
            string2 = string2.toLocaleLowerCase();
        }
        else {
            string1 = string1.toLowerCase();
            string2 = string2.toLowerCase();
        }
    }

    return string1 === string2;
}
click below button to copy the code. By JavaScript tutorial team

Solution 4:

With the help of regular expression also we can achieve.

(/keyword/i).test(source)
click below button to copy the code. By JavaScript tutorial team

we is for ignore case. If not necessary we can ignore and test for NOT case sensitive match like

(/keyword/).test(source)

click below button to copy the code. By JavaScript tutorial team

Solution 5:

There are two ways for case insensitive comparison:

Convert strings to upper case and then compare them using the strict operator (===). How strict operator treats operands read stuff

Pattern matching using string methods:

Use the "search" string method for case insensitive search. Read about search and other string methods

<!doctype html>
  <html>
    <head>
      <script>

        // 1st way

        var a = "apple";
        var b = "APPLE";  
        if (a.toUpperCase() === b.toUpperCase()) {
          alert("equal");
        }

        //2nd way

        var a = " Null and void";
        document.write(a.search(/null/i)); 

      </script>
    </head>
</html>

click below button to copy the code. By JavaScript tutorial team

Related Searches to javascript tutorial - Javascript case insensitive string comparison