How can I split a string based on elements of an array?

let example= [“This”,“is”,“an”,“example”];
let stringEx = “exampleanis”;
//expected result [“example”,“an”,“is”] and it is related to the example array.

That’s basically what I want to achieve.

You can use regex.

I would google search something like “Split a string based on multiple delimiters”

1 Like

But I don’t have a delimiter in this instance “exampleanis”.
There is no space.

Take a look at this article.

Can I do something like this?
const operations=["+","-","*","/",".","(",")"];
38 | const numbers=[0,1,2,3,4,5,6,7,8,9];
39 | let allOfThem=[…variables,…operations,…numbers];
40 | var regex = new RegExp("("+allOfThem.join("|")+")", ‘gi’);

let example= ["This","is","an","example"];
let stringEx = "exampleanis";
//expected result [“example”,“an”,“is”] and it is related to the example array.
function finditout(array,string){
 let result=[];
 array.forEach(function(str){
 if (string.indexOf(str)!=-1){
 result.push(str);
 }
 })
 return result;
}
finditout(example,stringEx)

Is this what you want?
I just search the array and push the elements which is in the string to a new array…
Oh,the order is a problem,use indexOf to resort the array can get the expected result.