Junior Mayhe
Junior Mayhe

Reputation: 16411

Using Javascript regular expression for filtering HTML

Good morning

is there a clever way to use a javascript regular expression to clean up this string:

var spans = "<SPAN><A href="javascript:sel.RemoveProductFromSelection('12372');"><IMG src="remove.gif" width=18> </A>Mirage - JOY</SPAN>
<SPAN><A href="javascript:sel.RemoveProductFromSelection('12427');"><IMG src="remove.gif" width=18> </A>Nikon - D40 </SPAN>
<SPAN><A href="javascript:sel.RemoveProductFromSelection('12438');"><IMG src="remove.gif" width=18> </A>Mitsuca - DS 6083 </SPAN>"

and produce a resulting array like this:

var filtered = ["12372","12427","12438"];

the source html string in spans variable could be indented or not.

thanks in advance

Upvotes: 0

Views: 406

Answers (2)

Junior Mayhe
Junior Mayhe

Reputation: 16411

Hey Spudley, here is what I've got so far

var filtered= spans.match(/\('(\d+)'\)/g);
for (var k=0;k<filtered.length;k++){
   filtered[k] = filtered[k].match(/\d+/g);
   alert(filtered[k]);
}

Upvotes: 0

Spudley
Spudley

Reputation: 168685

I'd suggest matching just the script portion. Something like this:

regex = /RemoveProductFromSelection\('(\d+)'\)/g;

while (match = regex.exec(spans)) {
   console.dir(match);
}

note: I've used console.dir() here to output the results in an easy to read format. console.dir() is a debugging function in Firebug; if you're not using Firebug, you'll need to use a different method to see the results.

Upvotes: 1

Related Questions