提取HTML标签的正则表达式解决方案(圣诞快乐,强烈推荐)
<font color=red>
<hr size=1>
</table>
上面这样的标签当然很好,可是如果混合了 Javascript 的标签或者有自定义属性的标签,提取起来就有很大难度,比如下面这样的:
<IMG UBB SRC="/ucgi/ubb_view.cgi?ubb=view;up=AllCHN;f=13;file=45.jpg" ALIGN="right" BORDER="0" onload="javascript:if(this.style.width>600){this.style.width=600;this.style.cursor='hand';}" onclick="javascript:if(this.style.cursor=='hand'){var nw=window.open('about:blank','_blank','');nw.document.open();nw.document.write('<img src='+this.src+'>');nw.document.close();nw.document.title='查看';nw=null;}" name="TopicImg" dragEnabled />
这里提供一个正则表达式,可以解决这个问题:
/<\/?\w+(\s+\S+?(=([\'\"]?).*?[^\\]\3)?)*\W*>/
如果你有更好的方法,或者上面的表达式有所遗漏,请交流。