user3674148 user3674148 - 7 months ago 19
Javascript Question

Split string on <a> tag

I would like to split the below value:

var stringValue = "<a href="/DE/Company/OurOrganisation/ActionsDE.pdf">DE</a><a href="/EN/Company/OurOrganisation/Act‌​ionsEN.pdf">EN</a><a href="/FR/Company/OurOrganisation/Act‌​ionsFR.pdf">FR</a>";


As we see, there are 3 anchors hence I have tried

1. var result = stringValue.split("<a/>");

2. var myRegexp = new RegExp("<a/>");
var result = stringValue.split(myRegexp);


What would be the best regex or way to get these 3 anchors ?

Answer

here's a pure regex solution:

var stringValue = '<a href="/DE/Company/OurOrganisation/ActionsDE.pdf">DE</a><a href="/EN/Company/OurOrganisation/Act‌ionsEN.pdf">EN</a><a href="/FR/Company/OurOrganisation/Act‌ionsFR.pdf">FR</a>';

var ar = stringValue.match(/<a.*?<\/a>/g)

console.log(ar)