Reputation: 1849
I need to check if a string has one of three substrings, and if yes, to implement a function. I know I can check for one substring using if (str.indexOf("term1") >= 0)
but is there a way to check for multiple substrings short of using several instances of this code?
TIA
Upvotes: 16
Views: 34696
Reputation: 5976
The .map()
function can be used to convert an array of terms into an array of booleans indicating if each term is found. Then check if any of the booleans are true
.
Given an array of terms
:
const terms = ['term1', 'term2', 'term3'];
This line of code will return true
if string
contains any of the terms
:
terms.map((term) => string.includes(term)).includes(true);
Three examples:
terms.map((term) => 'Got term2 here'.includes(term)).includes(true); //true
terms.map((term) => 'Not here'.includes(term)).includes(true); //false
terms.map((term) => 'Got term1 and term3'.includes(term)).includes(true); //true
Or, if you want to wrap the code up into a reusable hasTerm()
function:
const hasTerm = (string, terms) =>
terms.map(term => string.includes(term)).includes(true);
hasTerm('Got term2 here', terms); //true
hasTerm('Not here', terms); //false
hasTerm('Got term1 and term3', terms); //true
Try it out:
https://codepen.io/anon/pen/MzKZZQ?editors=0012
.map()
documentation:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
Note: This answer optimizes for simplicity and readability. If extremely large arrays of terms are expected, use a loop that short-circuits once a term is found.
Upvotes: 4
Reputation: 51
If you want to check for multiple string matches and highlight them, this code snippet works.
function highlightMatch(text, matchString) {
let textArr = text.split(' ');
let returnArr = [];
for(let i=0; i<textArr.length; i++) {
let subStrMatch = textArr[i].toLowerCase().indexOf(matchString.toLowerCase());
if(subStrMatch !== -1) {
let subStr = textArr[i].split('');
let subStrReturn = [];
for(let j=0 ;j<subStr.length; j++) {
if(j === subStrMatch) {
subStrReturn.push('<strong>' + subStr[j]);
} else if (j === subStrMatch + (matchString.length-1)){
subStrReturn.push(subStr[j] + '<strong>');
} else {
subStrReturn.push(subStr[j]);
}
}
returnArr.push(subStrReturn.join(''));
} else {
returnArr.push(textArr[i]);
}
}
return returnArr;
}
highlightMatch('Multi Test returns multiple results', 'multi');
=> (5) ['<strong>Multi<strong>', 'Test', 'returns', '<strong>multi<strong>ple', 'results']
Upvotes: 0
Reputation: 348
This achieves dynamically and elegantly what you are trying to do
const terms = ["term1", "term2", "term3"]
const str = "very large string to check for term1, tern2, etc ..."
// check if the string has some of the terms
const result1 = terms.some(term => str.includes(term))
// check if the string has all the terms
const result2 = terms.every(term => str.includes(term))
This also makes it easy to filter an array of strings for an array of substrings
const terms = ["term1", "term2", "term3"]
const strings = ["very large string text ....", "another large string text"]
// filter the strings of the array that contain some of the substrings we're looking for
const result1 = strings.filter(str => terms.some(term => str.includes(term)))
// filter the strings of the array that contain all the substrings we're looking for
const result2 = strings.filter(str => terms.every(term => str.includes(term)))
Upvotes: 32
Reputation: 388316
You can do something like
function isSubStringPresent(str){
for(var i = 1; i < arguments.length; i++){
if(str.indexOf(arguments[i]) > -1){
return true;
}
}
return false;
}
isSubStringPresent('mystring', 'term1', 'term2', ...)
Upvotes: 2
Reputation: 48415
You could use a loop. Maybe even create a helper function like so:
function ContainsAny(str, items){
for(var i in items){
var item = items[i];
if (str.indexOf(item) > -1){
return true;
}
}
return false;
}
Which you can then call like so:
if(ContainsAny(str, ["term1", "term2", "term3"])){
//do something
}
Upvotes: 10
Reputation: 5632
Maybe this:
if (str.indexOf("term1") >= 0 || str.indexOf("term2") >= 0 || str.indexOf("term3") >= 0)
{
//your code
}
Upvotes: 3