javascript includes() case insensitive
You can create a RegExp from filterstrings
first
var filterstrings = ['firststring','secondstring','thridstring'];
var regex = new RegExp( filterstrings.join( "|" ), "i");
then test
if the passedinstring
is there
var isAvailable = regex.test( passedinstring );
You can simply convert the passedinstring to lowercase.
var passedinstring = localStorage.getItem("passedinstring").toLowerCase();
ES6 array method filter() can simply the solution in single line, use includes() method to determines whether an array includes a certain value among its entries in conjunction to the toLowerCase() method to convert it to lowercase.
var filterstrings = ['firststring','secondstring','thridstring'];
var passedinstring = localStorage.getItem("passedinstring");
// convert each string from filterstrings and passedinstring to lowercase
// to avoid case sensitive issue.
filteredStrings = filterstrings.filter((str) => str.toLowerCase().includes(passedinstring.toLowerCase())
convert filterstring and passedinstring to lowercase and compare
var filterstrings = ['firststring','secondstring','thridstring'];
var passedinstring =
localStorage.getItem("passedinstring").toLowerCase();
for (i = 0; i < filterstrings.lines.length; i++) {
if (passedinstring.includes(filterstrings[i].toLowerCase())) {
alert("string detected");
}
}
Fixed case sensitivity issue using toLowerCase()
. It turns all the string
to lower case while comparing.
var product=productList.filter((x) => x.Name.toLowerCase().includes(("Active").toLowerCase()))