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)
ES2015 findIndex:
var array = ['I', 'hAve', 'theSe', 'ITEMs'],
indexOf = (arr, q) => arr.findIndex(item => q.toLowerCase() === item.toLowerCase());
console.log( indexOf(array, 'i') ) // 0
console.log( indexOf(array, 'these') ) // 2
console.log( indexOf(array, 'items') ) // 3
In ECMA-262, 5th edition, you could use Array.prototype.some for this.
var array = [ 'I', 'hAve', 'theSe', 'ITEMs' ];
var query = 'these'.toLowerCase();
var index = -1;
array.some(function(element, i) {
if (query === element.toLowerCase()) {
index = i;
return true;
}
});
// Result: index = 2
I would suggest using a Map instead of an array for your todo list. A Map has the advantage that it provides key-based look-up in constant time, and does not store duplicate entries with the same key. You could then use the lower case variant as the key, and the original (mixed-case) string as the value for that key.
You could define todos as a Map instead of an array, and use the lower case string as the key:
constructor(props) {
super(props);
this.todos = new Map();
this.addToDo('Get Up from bed');
this.addToDo('Eat Breakfast');
}
Then, to add the task becomes very straightforward, as a Map overwrites duplicates:
addToDo() {
this.todos.set(task_title.toLowerCase(), task_title);
this.setState({
todos: this.todos
});
}
In rendering you would need to use Array.from(..., <map-function>), as .map is not defined for Map objects:
<div className="todos">
{Array.from(this.todos, this.eachTodo)}
</div>
Which means the eachToDo method will receive a pair of strings (an array), instead of a string, and we want to display the second of the pair, so using index [1]:
eachTodo(task, i) {
return (
<Todo key={i} index={i}>{task[1]}</Todo>
)
}
No, the only built-in methods are case sensitive. You'll need that loop and either toLowerCase or a case-insensitive regular expression (perhaps tucked away in a useful reusable function).