简体   繁体   English

使用Javascript正则表达式过滤HTML

[英]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: 有没有一种聪明的方法来使用javascript正则表达式来清理此字符串:

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. spans变量中的源html字符串可以缩进还是不缩进。

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()以易于阅读的格式输出结果。 console.dir() is a debugging function in Firebug; console.dir()是Firebug中的调试功能; if you're not using Firebug, you'll need to use a different method to see the results. 如果您不使用Firebug,则需要使用其他方法来查看结果。

Hey Spudley, here is what I've got so far 嘿Spudley,这是到目前为止我得到的

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM