Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery/javascript check string for multiple substrings

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

like image 654
Phil Avatar asked Mar 04 '13 12:03

Phil


People also ask

How do you I check if multiple characters are in string Javascript?

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. }

How do I check if a string contains multiple substrings?

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.


3 Answers

if (/term1|term2|term3/.test("your string")) {
   //youre code
}
like image 129
elrado Avatar answered Nov 02 '22 09:11

elrado


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)))
like image 38
Yassine K Avatar answered Nov 02 '22 08:11

Yassine K


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:

  1. 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.
  2. To support IE, transpile to replace occurrences of .includes(x) with .indexOf(x) !== -1 and => with a function declaration.
like image 32
Dem Pilafian Avatar answered Nov 02 '22 09:11

Dem Pilafian