| by Arround The Web | No comments

How to Case Insensitive String Comparison in JavaScript

While writing the code, a developer may often need to compare two strings to ensure that specific tasks are completed. Comparing strings without paying attention to their cases such as capital and lowercase letters are known as a case-insensitive comparison. Many languages support string comparison with case sensitivity and insensitivity.

This article will illustrate the methods for case-insensitive comparison of strings in JavaScript.

How to Case Insensitive String Comparison in JavaScript?

For case-insensitive comparison of strings in JavaScript, use the following predefined methods:

    • localeCompare() method
    • toUpperCase() and toLowerCase() method
    • Regular expression with test() method

Let’s look at the working of each of the above-mentioned approaches separately.

Method 1: Case Insensitive String Comparison Using LocaleCompare() Method

The case-insensitive comparison of strings uses the “localeCompare()” method. This method returns a number (positive, negative, or zero). The strings are compared in a sorting order, if the reference string is longer than the comparison string or it comes after the compared string, it gives a positive number. If the reference string is shorter, or comes before the compared string, it returns a negative number. A zero should be returned if the reference string is equivalent to the compared string.

Syntax

Follow the given-provided syntax for the localeCompare() method:

string1.localeCompare(string2, locales, options)

 
Here,

    • string2” is the compare string, where string 1 will be compared.
    • locales” is the language tag.
    • options” are the locale provided by the user while working with it.

Example

Create two strings “string1” and “string2”, with strings “linuxhint” and “LinuxHint” respectively:

var string1 = 'linuxhint';
var string2 = 'LinuxHint';

 
Compare string1 with string2 using the “localeCompare()” method and store the result in a variable “comp”. The third argument of the method will set as “sensitivity: ‘base’” that indicates the compared strings base letters are not different:

var comp = string1.localeCompare(string2, undefined, { sensitivity: 'base' })

 
In conditional statement, check if the returned value of localeCompare() method is equivalent to zero, it prints “Strings are equal”, else, “Strings are not equal”:

if(comp == 0){
 console.log("Strings are equal");
} else {
 console.log("Strings are not equal");
}

 
Output

Method 2: Case Insensitive String Comparison Using toUpperCase() and toLowerCase() Method

The most used approaches for comparing case insensitive strings are toUpperCase() method or toLowerCase() Method. They convert the strings into uppercase or lowercase and then compare them with the help of strict equality operators.

Syntax

For toUpperCase() method, use the following method:

string.toUpperCase();

 
Use the below syntax for toLowerCase() method.

string.toLowerCase();

 
Example: Case Insensitive String Comparison Using toUpperCase() Method

Consider the above created strings “string1” and “string2” and then compare them using the toUpperCase() method with strict equality operator:

if(string1.toUpperCase() === string2.toUpperCase()){
 console.log("Strings are equal");
} else {
 console.log("Strings are not equal");
}

 
The output indicates both the strings are equal by ignoring case:


Example: Case Insensitive String Comparison Using toLowerCase() Method

Here, the strings are compared using the toLowerCase() method that will first convert the strings into lower case and then compare them using the === operator:

if(string1.toLowerCase() === string2.toLowerCase()){
 console.log("Strings are equal");
} else {
 console.log("Strings are not equal");
}

 
The corresponding output will be:

Method 3: Case-Insensitive String Comparison Using Regular Expression With test() Method

The predefined JavaScript “test()” method, which uses a regular expression, is another way to compare two strings. Based on the specified regex, it determines whether strings are equal or not.

Syntax

Follow the given syntax for using a regular expression to compare the strings:

RegExp(string, "gi")

 
Here,

    • RegExp” stands for “regular expression”.
    • g” is the global variable that allows checking all the strings.
    • i” is a flag variable that indicates a case should be ignored while using a string to match the pattern.

Example

First, create a new object of the RegExp() by passing a string and the regular expression as an argument:

var comp = new RegExp(string1, "gi");

 
Compare the strings using the test() method:

if(comp.test(string2)){
 console.log("Strings are equal");
} else {
 console.log("Strings are not equal");
}

 
Output

Conclusion

To compare case insensitive strings in JavaScript, use the JavaScript predefined methods including localeCompare() method, toUpperCase() and toLowerCase() method, or the test() method with Regular expression. The “toUpperCase() and toLowerCase()” methods are the most used approach for comparing two case-insensitive strings. This article illustrated the different ways for case-insensitive comparison of strings in JavaScript.

Share Button

Source: linuxhint.com

Leave a Reply