日期:2014-05-18  浏览次数:20698 次

经典正则匹配一个html标记问题
要截取标记的整个内容:
如:
<TABLE   cellSpacing=0   cellPadding=0> <TBODY> <TR> <TD   class=esriTocLabel   noWrap> <A   href= 'javascript:void(0);parent.localandinfo(1_0);void(0); '> <SPAN> 事实上生生世世上 </SPAN> </A> </TD> </TR>
TBODY> </TABLE>
这个table中的 <a> </a> 中间的字符串,有没有简单的方法

------解决方案--------------------
Pattern pattern = Pattern.compile( " <\\s*[aA]\\s*[^> ]*> | <\\s*[aA]\\s*> | </[aA]> ");
String[] array = pattern.split(source);
for (int i = 0; i < array.length; i++)
{
System.out.println(array[i]);
}
------解决方案--------------------
<script>
var str = " <TABLE cellSpacing=0 cellPadding=0> <TBODY> <TR> <TD class=esriTocLabel noWrap> <A href= 'javascript:void(0);parent.localandinfo(1_0);void(0); '> <SPAN> 事实上生生世世上 </SPAN> </A> </TD> </TR> </TBODY> </TABLE> ";
var re = / <a.*?> (.*) <\/a> /ig;
var a = str.match(re);
alert(RegExp.$1);
</script>
这样做。
------解决方案--------------------
如果有多个a标签,像下面这样做:
<script>
var str = " <TABLE cellSpacing=0 cellPadding=0> <TBODY> <TR> <TD class=esriTocLabel noWrap> <A href= 'javascript:void(0);parent.localandinfo(1_0);void(0); '> <SPAN> 事实上生生世世上 </SPAN> </A> </TD> </TR> </TBODY> </TABLE> <A href= 'javascript:void(0);parent.localandinfo(1_0);void(0); '> <SPAN> sdfasdf </SPAN> </A> ";
var re = / <a.*?> (.*?) <\/a> /ig;
var a = str.match(re);
for (var i = 0; i < a.length; i++)
{
a[i].match(re);
alert(RegExp.$1);
}
</script>
------解决方案--------------------
public static String getHrefAndSpan(String href){
Pattern s=Pattern.compile( " <A.*> .* </A> ");
Matcher m=s.matcher(href);
if(m.find())
return m.group();
else
return null;
}
这个就可以得到
<A href= 'javascript:void(0);parent.localandinfo(1_0);void(0); '> <SPAN> 事实上生生世世上 </SPAN> </A>