[Javascript] How to perform a case insensitive comparison of two strings?


I want to compare the strings and check if they are same irrespective of the case.How do I perform a case insensitive comparison of two strings in JavaScript?

1 Answer

3 years ago by

Use either of the below methods to check if two strings are equal

let str1 = "Hello";
let str2 = "heLLo";

// using toUppercase(works except for special Unicode characters)
if(str1.toUpperCase() === str2.toUpperCase()) {
  console.log("strings are matched");
}

//using Regex
let regex = new RegExp(str2 , 'i');
if (regex.test(str1)) {
  console.log("strings are matched");
}

Run here https://onecompiler.com/javascript/3xns5hzgp

3 years ago by Meera