DEV Community

byby
byby

Posted on • Originally published at byby.dev

Compare two strings ignoring case in JavaScript

Strings are one of the most common data types in JavaScript, used to represent text and characters. String comparison is essential for tasks such as sorting data, searching for specific patterns, validating user inputs, and making decisions based on textual data.

When comparing strings in JavaScript, you need to care about the following aspects:

  • The case of the letters, "Hello" and "hello" are not equal.
  • The type of the values, "1" and 1 are equal with ==, but not with ===.
  • The locale of the strings, "a" and "ä" are equal in some locales, but not in others.

The choice between case-sensitive and case-insensitive comparison depends on the specific requirements of your program. Sometimes, you may want to treat "abc" and "ABC" as different values, while in other cases, you may want them to be considered the same.

You can use one of following ways to compare two strings, ignoring the case:

  • Converting strings to the same case
  • Using locale compare method
  • Using regular expressions

You should choose the method that best suits your needs and preferences, considering the characters and the languages of the strings, the purpose and the context of the comparison, and the performance and the reliability of the method.

Converting strings to the same case

You can convert both strings to either lower case or upper case using toLowerCase() or toUpperCase(), and then compare them using the strict equality operator (===).

const str1 = "Bye bye";
const str2 = "bye bye";

const c0 = str1 === str2;
console.log(c0); // false

// Convert both strings to lower case and compare
const c1 = str1.toLowerCase() === str2.toLowerCase();
console.log(c1); // true

// Convert both strings to upper case and compare
const c2 = str1.toUpperCase() === str2.toUpperCase();
console.log(c2); // true
Enter fullscreen mode Exit fullscreen mode

However both toLowerCase() and toUpperCase() do not handle locale well. They may not work correctly for some characters that have different cases in different languages or alphabets, such as the letter "i" in Turkish. They may also not handle accents, diacritics, or other variations of the same letter, such as the letters "a" and "ä" in German.

Luckily, you can use the toLocaleLowerCase() and toLocaleUpperCase() methods to convert the case of a string according to a specific locale. For example, to convert the letter "i" in Turkish, you can use the "tr" locale:

// Letter i in Turkish
const str1 = "İSTANBUL";
const str2 = "istanbul";

const c0 = str1 === str2;
console.log(c0); // false

// Using toLowerCase
const c1 = str1.toLowerCase() === str2.toLowerCase();
console.log(c1); // false

// Using toLocaleLowerCase
const c2 = str1.toLocaleLowerCase("tr") === str2.toLocaleLowerCase("tr")
console.log(c2); // true
Enter fullscreen mode Exit fullscreen mode

Using locale compare method

JavaScript has a localeCompare() method that compares two strings according to the language rules of a specific locale. This method can handle different alphabets, accents, sorting orders by using the options parameter sensitivity to specify how you want to compare the strings.

  • base: Only the base characters of the strings are compared, ignoring accents, case, and other differences. For example, "a" and "ä" are considered equal with this option.
  • accent: The base characters and accents of the strings are compared, ignoring case and other differences. For example, "a" and "A" are considered equal with this option, but not "a" and "ä".
  • case: The base characters and case of the strings are compared, ignoring accents and other differences. For example, "a" and "ä" are considered equal with this option, but not "a" and "A".
  • variant: The base characters, accents, case, and other differences of the strings are compared. This is the most strict option. For example, "a", "A", "ä", and "Ä" are all considered different with this option.

The result value of localeCompare is a number that indicates the relative order of two strings in a specific locale: -1 (before), 0 (equal), 1 (after).

Both "accent" and "base" options ignore the case of the letters when comparing two strings, but you may want to use the "accent" when you need to distinguish between strings that have different accents, and "base" if otherwise.

const str1 = "Café";
const str2 = "cafe";

// Using default sensitivity (variant)
const c0 = str1.localeCompare(str2);
console.log(c0); // 1

// Using the accent sensitivity
const c1 = str1.localeCompare(str2, undefined, { sensitivity: "accent" });
console.log(c1); // 1,

// Using the base sensitivity
const c2 = str1.localeCompare(str2, undefined, { sensitivity: "base" });
console.log(c2); // 0
Enter fullscreen mode Exit fullscreen mode

Using regular expressions

You can use regular expressions with the i flag, which means it will ignore the case of the characters in the pattern. You can create a regular expression from a string using the RegExp constructor, and then use the test() method to check if it matches another string.

var str1 = "Hello";
var str2 = "hello";

// Create a regular expression from str1 with the i flag
var regex = new RegExp("^" + str1 + "$", "i");

// Test if the regular expression matches str2
const c = regex.test(str2);
console.log(c); // true
Enter fullscreen mode Exit fullscreen mode

However, using regular expressions can be tricky, because you need to escape any special characters in the string that have a special meaning in regular expressions, such as ., *, +, ?, etc. Otherwise, you may get unexpected results.

var str1 = "[hello]";
var str2 = "[Hello]";

// Create a regular expression from str1 with the i flag
var regex = new RegExp("^" + str1 + "$", "i");

// Test if the regular expression matches str2
const c = regex.test(str2);
console.log(c); // false, because [ and ] are special characters in regular expressions
Enter fullscreen mode Exit fullscreen mode

To avoid this problem, you can use a function to escape any special characters in the string before creating a regular expression from it. For example:

function escapeRegExp(string) {
  return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
}

var str1 = "[hello]";
var str2 = "[Hello]";

// Escape any special characters in str1
var escapedStr1 = escapeRegExp(str1);

// Create a regular expression from escapedStr1 with the i flag
var regex = new RegExp("^" + escapedStr1 + "$", "i");

// Test if the regular expression matches str2
const c = regex.test(str2);
console.log(c); // true
Enter fullscreen mode Exit fullscreen mode

This method may not handle locale well, depending on the characters and the flags used. Regular expressions are patterns that can match or test strings based on certain rules and symbols. However, regular expressions may not be aware of the differences or similarities between characters in different languages or alphabets, such as accents, diacritics, or case.

Top comments (1)

Collapse
 
wearypossum4770 profile image
Stephen Smith • Edited

This is expertly written, good job. I moderated you post and put you at "mid-level" this isn't beginner level too many options or a beginner.