开发者

Using Javascript regular expression for filtering HTML

开发者 https://www.devze.com 2023-01-22 05:26 出处:网络
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\');\">&

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 sr开发者_开发知识库c="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


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消