I've read many question but haven't found the desired one.
I have an array of words .
How can I split the string (using regex ) by the words that in the array ?
Example
var a=['john','paul',...];
var s = 'The beatles had two leaders , john played the guitar and paul played the bass';
My desired result is an array :
['The beatles had two leaders , ' , ' played the guitar and ','played the bass']
So basically john and paul are the splitters.
What have I tried :
I've managed to this :
var a='The beatles had two leaders , john played the guitar and paul played the bass'
var g= a.split(/(john|paul)/)
console.log(g)
Result :
["The beatles had two leaders , ", "john", " played the guitar and ", "paul", " played the bass"]
But I don't want that paul and john to be in results
Question:
How can I split a string via array of words using regex ?
NB if there are many john
, split by the first.