javascript string case insensitive code example

Example 1: Javascript case insensitive string comparison

var name1 = "Taylor Johnson";
var name2 ="taylor johnson";

//convert to lowercase for case insensitive comparison
if(name1.toLowerCase() === name2.toLowerCase()){
    //names are the same
}

Example 2: javascript string search case insensitive

function stringContainsCaseInsensitive(search, subject){
    return subject.toLowerCase().indexOf(search.toLowerCase()) !== -1;
}

stringContainsCaseInsensitive("red","We painted the room red.");//true

Example 3: js includes case insensitive

var passedinstring = localStorage.getItem("passedinstring").toLowerCase();

Example 4: compare string camelcase and lowercase javascript

var haystack = "A. BAIL. Of. Hay.";
var needle = "bail.";
var needleRegExp = new RegExp(needle.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"), "i");
var result = needleRegExp.test(haystack);
if (result) {
    // Your code here
}