日期:2014-05-16  浏览次数:20565 次

正则表达式难题!!!
如何获取一段html中包含特定字符的某个标签对!

如:
<!DOCTYPE   HTML   PUBLIC   "-//W3C//DTD   HTML   4.0   Transitional//EN ">
<HTML>
  <HEAD>
    <TITLE>   New   Document   </TITLE>
    <META   NAME= "Generator "   CONTENT= "EditPlus ">
    <META   NAME= "Author "   CONTENT= " ">
    <META   NAME= "Keywords "   CONTENT= " ">
    <META   NAME= "Description "   CONTENT= " ">
  </HEAD>

  <BODY>
    <TABLE>
    <TR>
<TD> asdf </TD>
<TD> asdf </TD>
    </TR>
    <TR>
<TD> #asdf </TD>
<TD> asdf </TD>
    </TR>
    <TR>
<TD> sdf </TD>
<TD> asdf </TD>
    </TR>
    <TR>
<TD> sdf </TD>
<TD> asdf </TD>
    </TR>
    </TABLE>
  </BODY>
</HTML>

我要获取
<TR>
<TD> #asdf </TD>
<TD> asdf </TD>
    </TR>

------解决方案--------------------
HTML code
<!DOCTYPE   HTML   PUBLIC   "-//W3C//DTD   HTML   4.0   Transitional//EN "> 
<HTML> 
  <HEAD> 
    <TITLE>   New   Document   </TITLE> 
    <META   NAME= "Generator "   CONTENT= "EditPlus "> 
    <META   NAME= "Author "   CONTENT= " "> 
    <META   NAME= "Keywords "   CONTENT= " "> 
    <META   NAME= "Description "   CONTENT= " "> 
  </HEAD> 

  <BODY> 
    <TABLE> 
    <TR> 
<TD> asdf </TD> 
<TD> asdf </TD> 
    </TR> 
    <TR> 
<TD> #asdf </TD> 
<TD> asdf </TD> 
    </TR> 
    <TR> 
<TD> sdf </TD> 
<TD> asdf </TD> 
    </TR> 
    <TR> 
<TD> sdf </TD> 
<TD> asdf </TD> 
    </TR> 
    </TABLE> 
  </BODY> 
</HTML> 

<script>
window.onload=function(){
    var obj=document.body;
    alert(obj.innerHTML.match(/<tr(?![^#]+<\/tr>)[#\s\S]*?<\/tr>/gim));
}
</script>