search a string for an array of string fragments

Question:

I need to search through a string to see if it contains any of the text in an array of strings. For example

excludeList=”warning”,”a common unimportant thing”, “something else”

searchString=here is a string telling us about a common unimportant thing.

otherString=something common but unrelated

In this example, we would find the “common unimportant thing” string from the array in my searchList and would return true. however otherString doesn’t contain any of the complete strings in the array, so would return false.

Im sure this isnt that complicated, but I’ve been looking at it for too long…

Update:
The best I can get so far is:

it isnt too elegant though…

Answer:

You can use a regular expression. The ‘|’ regex character is the equivalent to the OR operator:

Source:

search a string for an array of string fragments by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply