If just looking for a single character in a set of strings, I would think basic string manipulation would be sufficient.
What is not clear to me from your question is why you are using word boundaries in your regex definition when the array itself is just single words. Is your intent to analyze individual words in each of the array strings (with string perhaps consisting of multiple words) or are you only truly going to have single word string in the array?
For now, I will provide answer given the assumption of an array of single words.
To do that, I would simply use Array.filter()
in combination with String.indexOf()
.
var needle = 'e';
var haystack = ["platypus", "elvis", "javascript", "stackoverflow"];
var filtered = haystack.filter( str => (str.indexOf(needle) === -1) );
Based on your comment that array entries could have multiple words, I would suggest using Array.reduce()
to build all array of all words across all entries that do not have have the needle character in them.
var needle = 'e';
// note global flag here to get all matches
var regex = new RegExp('\\b([^' + needle + ']+)\\b', 'g');
var haystack = ["platypus", "elvis", "javascript", "stackoverflow", "some multi-word string"];
var result = haystack.reduce(
(aggregator, str) => {
while (match = regex.exec(str) !== null) {
aggregator.push(match[1]);
}
},
[]
);
console.log(result); // ["platypus", "javascript", "multi-word", "string"]