求一正则表达式取得特定内容

bugattieb164 2007-05-21 11:08:25
我用XMLHTTP获取了别人网站的返回的一段HTML代码,这其中包括有head,body,title等标签.完整的HTML代码.据观察发现在代码中有一ID名为mytable的表格,该表格的内容正是我所需要的.其他的就不要了.如何取得该ID名为mytable表格的内容呢?
...全文
240 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
webphoenix 2007-05-21
  • 打赏
  • 举报
回复
sorry,刚写错了正则应该是这样的:

/(?:<)(table)(?:[^>]*)(?:id="mytable")(?:[^>]*)(?:>)(?:.*)(?:<\/\1>)/g

----------例子---

<!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>
<script language="JavaScript">
<!--
var string = '<html><head></head><body><div>测试测试</div><table id="mytable" border="1"><tr><td>table content</td></tr></table></body></html>';
var re = /(?:<)(table)(?:[^>]*)(?:id="mytable")(?:[^>]*)(?:>)(?:.*)(?:<\/\1>)/g;
//alert(re.test(string));
while ((arr = re.exec(string)) != null){
alert(string.substring(arr.index,arr.lastIndex));
}

//-->
</script>
</body>
</html>
jinjuduo 2007-05-21
  • 打赏
  • 举报
回复
<textarea name=textarea1>
<table ID=mytable >23445<td>123dsfds</td></table>
</textarea>
<script>
//var str=textarea1.value;
re=/<table[\s\S]*ID=mytable[\s\S]*?>([\s\S]*?)<\/table>/ig;
alert(aaa(textarea1.value))
function aaa(x)
{
var rtn="";
var arr = x.match(re);
for(i=0;i<arr.length;i++)
{
rtn += arr[i].replace(re,"$1") + "\n";
}
return(rtn);
}
</script>
bugattieb164 2007-05-21
  • 打赏
  • 举报
回复
webphoenix() 请问你试过吗?我试怎么不行啊.
webphoenix 2007-05-21
  • 打赏
  • 举报
回复
这样写:

var reg = /<(table)(?:[^>]*)(?:id="mytable")(?:[^>]*)><\/\1>/g;
while ((arr = re.exec(string)) != null){
alert(arr.input);
}
bugattieb164 2007-05-21
  • 打赏
  • 举报
回复
jinjuduo().你的代码不行啊,这样测试虽然可以,但只要中间还有其他HTML标签就失效,你要知道TABLE中肯定会有TD,TR等标签,请你完整测试一下.
jinjuduo 2007-05-21
  • 打赏
  • 举报
回复
我用XMLHTTP获取了别人网站的返回的一段HTML代码,这其中包括有head,body,title等标签.完整的HTML代码.据观察发现在代码中有一ID名为mytable的表格,该表格的内容正是我所需要的.其他的就不要了.如何取得该ID名为mytable表格的内容呢?
<td ID=mytable >23445</td>
</textarea>
<script>
var str=textarea1.value;
re=/<td[\s\S]*ID=mytable[\s\S]*>([\s\S]*?)<\/td>/ig;
str.match(re);
//str=str.replace(re,"");
str=RegExp.$1;
alert( str);
</script>
chouchy 2007-05-21
  • 打赏
  • 举报
回复
截取返回的HTML代码。
yixianggao 2007-05-21
  • 打赏
  • 举报
回复
var oTargetTable = document.getElementById("mytable");
alert(oTargetTable.outerHTML); // 这就是你想要的
yixianggao 2007-05-21
  • 打赏
  • 举报
回复
document.getElementById("mytable");

87,993

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧