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
let str = "Here is the string in which we want to find multiple substrings." let subStrArr = ["Here is", "which we", "multiple"] let containsAllSubStrs = subStrArr. every(subStr => { return str. includes(subStr); }); if (containsAllSubStrs) { //containsAllSubStrs equals 'true', meaning all substrings were found. }
You can use any : a_string = "A string is more than its parts!" matches = ["more", "wholesome", "milk"] if any(x in a_string for x in matches): Similarly to check if all the strings from the list are found, use all instead of any . any() takes an iterable.
if (/term1|term2|term3/.test("your string")) {
//youre code
}
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)))
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
Notes:
.includes(x)
with .indexOf(x) !== -1
and =>
with a function
declaration.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With