I've an array(testArray
) of strings. I need to remove the strings from array which doesn't contain at least one of the searchTerms
.testArray
should ignore case of searchTerms
.
EDIT: Result array should include the string even the search term is part of a word in the string.
eg:"someright text"
should be included in the result.
var testArray = [
"I am",
"I am wrong and I don't know",
"I am right and I know",
"I don't know",
"some text",
"I do know"
],
searchTerms = ["I", "right","know"] //or ["i", "right","know"];
$.each(searchTerms, function(index, term) {
var regX = new RegExp(term, "i");
testArray = $.map(testArray, function(item) {
if (regX.test(item)) {
return item;
} else {
return;
}
});
});
console.log(testArray);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
The result should be as below,
testArray = [
"I am",
"I am wrong and I don't know",
"I am right and I know",
"I don't know",
"I do know"
]