I have a bunch of strings that typically looks something like this:
string 1<div>string 2<br></div>string 3
string 1<div>string 2<br></div><div>string 3<br></div>
<div>string 1<br></div><div>string 2<br></div><div>string 3<br></div>
And I need to extract the text (both inside and outside/between elements, as seen above) into an array like this:
['string 1', 'string 2', 'string 3']
Is there a way to do this in pure Javascript?
I tried something like this:
console.log(text.split(/<div>(.*)<br><\/div>/g))
But it only works for the first one:
[ 'string 1', 'string 2', 'string 3' ]
While it fails on the two last variations:
[ 'string 1', 'string 2<br></div><div>string 3', '' ]
[ '', 'string 1<br></div><div>string 2<br></div><div>string 3', '' ]
.match(/(?<=>|^)[^<]+/g), but if the inputs can vary in some other way, this fails catastrophically.>(or string start) and<(or string end), and nothing else is (at least no non-empty strings). It ignores everything else. Note, that look-behind isn't yet available everywhere, but if that is a problem, you can do something like match the>as well, and remove it afterwards.