Description
I'm attempting to extract URLS and/or CDATA from XML. The current solution I have works well, but only returns the first element. How do I return multiple elements with this specfic regex?
The XML is in the form of:
<MediaFile>
https://some_url.com/file.mp4
</MediaFile>
<MediaFile>
https://some_url2.com/file.mp4
</MediaFile>
and
<MediaFile>
<!CDATA some data here with spaces sometimes>
</MediaFile>
...etc
What I'm trying to achieve
In my example, there are 3 mediafile tags and I'm trying to extract 3 different URLS and CDATA. The final solution should look something like
1st url https://example1.com/file.mp4
2nd url https://example2.com/file.mp4
3rd url <!CDATA some data example>
What I've tried:
link to regex101
const data = `<MediaFile delivery="progressive" width="640" height="360" type="video/mp4" bitrate="397" scalable="false" maintainAspectRatio="false">https://example1.com/file.mp4</MediaFile><MediaFile delivery="progressive" width="1024" height="576" type="video/mp4" bitrate="1280" scalable="false" maintainAspectRatio="false">https://example2.com/file.mp4</MediaFile><MediaFile delivery="progressive" width="1024" height="576" type="video/mp4" bitrate="1280" scalable="false" maintainAspectRatio="false"><!CDATA some data example></MediaFile>`;
const regex = /<MediaFile[^>]*type="video\/mp4"[^>]*>([\s\S]*?)<\/MediaFile>/gm;
const res = regex.exec(data);
console.log('1st url', res[1]);
console.log('2nd url', res[2]);
console.log('3rd url', res[3]);