0

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

1
  • Is the string initially part of the document? It would probably be easier to extract the values while they are still in the DOM. Commented Oct 26, 2010 at 13:17

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.