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

求一正则表达式取得特定内容
我用XMLHTTP获取了别人网站的返回的一段HTML代码,这其中包括有head,body,title等标签.完整的HTML代码.据观察发现在代码中有一ID名为mytable的表格,该表格的内容正是我所需要的.其他的就不要了.如何取得该ID名为mytable表格的内容呢?

------解决方案--------------------
<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>

------解决方案--------------------
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>