You can create a RegExp from filterstrings first
var filterstrings = ['firststring','secondstring','thirdstring'];
var regex = new RegExp( filterstrings.join( "|" ), "i");
then test if the passedinstring is there
var isAvailable = regex.test( passedinstring );
Here is a test implementation:
// Take care to escape these strings!
// If they are user-input or may contain special regex characters (such as "$" or "."), they have to be escaped.
var filterStrings = ['one','two','three'];
var regex = new RegExp(filterStrings.join( "|" ), "i");
var sentences = [
"one", // true
"OnE", // true
"a rabbit", // false
"and then two rabbits", // true
"standing", // false
"prone", // true
"AoNeAtWo", // true
];
sentences.forEach(sentence => {
console.log(
regex.test(sentence) ? ' match:' : ' not a match:',
"'" + sentence + "'",
);
});
Answer from gurvinder372 on Stack OverflowYou can create a RegExp from filterstrings first
var filterstrings = ['firststring','secondstring','thirdstring'];
var regex = new RegExp( filterstrings.join( "|" ), "i");
then test if the passedinstring is there
var isAvailable = regex.test( passedinstring );
Here is a test implementation:
// Take care to escape these strings!
// If they are user-input or may contain special regex characters (such as "$" or "."), they have to be escaped.
var filterStrings = ['one','two','three'];
var regex = new RegExp(filterStrings.join( "|" ), "i");
var sentences = [
"one", // true
"OnE", // true
"a rabbit", // false
"and then two rabbits", // true
"standing", // false
"prone", // true
"AoNeAtWo", // true
];
sentences.forEach(sentence => {
console.log(
regex.test(sentence) ? ' match:' : ' not a match:',
"'" + sentence + "'",
);
});
ES6 array method filter() can simplify the solution to a single line. Use includes() method to determine 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())
Add .toUpperCase() after referrer. This method turns the string into an upper case string. Then, use .indexOf() using RAL instead of Ral.
if (referrer.toUpperCase().indexOf("RAL") === -1) {
The same can also be achieved using a Regular Expression (especially useful when you want to test against dynamic patterns):
if (!/Ral/i.test(referrer)) {
// ^i = Ignore case flag for RegExp
Another options is to use the search method as follow:
if (referrer.search(new RegExp("Ral", "i")) == -1) { ...
It looks more elegant then converting the whole string to lower case and it may be more efficient.
With toLowerCase() the code have two pass over the string, one pass is on the entire string to convert it to lower case and another is to look for the desired index.
With RegExp the code have one pass over the string which it looks to match the desired index.
Therefore, on long strings I recommend to use the RegExp version (I guess that on short strings this efficiency comes on the account of creating the RegExp object though)