+ -
当前位置:首页 → 问答吧 → 请教一个正则表达式的问题

请教一个正则表达式的问题

时间:2010-10-18

来源:互联网

我有一个这样结构的html
<html>
<body>
  <div id="1">
  <table><div></div></table>
  <table><div></div></table>
  <table><div></div></table>
  </div>
</body>
</html>


我想提取第一层div(即id=1的div)的源码,正则表达式怎么写呢~?

谢谢!!

作者: xhiaa   发布时间: 2010-10-18

var str="你的html"

var str = str.replace(/<div (?=id="1")[\s\S]*<\/div>/gi,"")

alert(str)

作者: aspwebchh   发布时间: 2010-10-18

var html="......";
var reg = /<div (?=id="1")[\s\S]*<\/div>/gi
alert(reg.exec(html))

作者: aspwebchh   发布时间: 2010-10-18

引用 1 楼 aspwebchh 的回复:
var str="你的html"

var str = str.replace(/<div (?=id="1")[\s\S]*<\/div>/gi,"")

alert(str)


呃,我试了下,不行哇。。。还是整段html源码……

作者: xhiaa   发布时间: 2010-10-18

最后的</div>错了…………
不好意思,我说漏了,结构应该是这样的

<html>
<body>
<div id="main">
  <div id="1">
  <table><div></div></table>
  <table><div></div></table>
  <table><div></div></table>
  </div>

  <div id="2">
  <table><div></div></table>
  <table><div></div></table>
  <table><div></div></table>
  </div>
</div>
</body>
</html

作者: xhiaa   发布时间: 2010-10-18