http://www.cnblogs.com/onlytiancai/archive/2005/07/15/193386.html js里的match方法和exec方法的区别,关于正则表达式的
代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>yaoye</title>
<style type="text/css">
<!--
.css_news {
font-size: 12pt;
text-align: left;
}
.divsty
{
font-size:8px;
color:#ccc;
}
-->
</style>
<script language="javascript">
function setData()
{
var str = document.getElementById('div1').innerHTML;
var re = /<a\s*?href\s*?=\s*?\"[^\"]*?id=(\d*?)\"><font\s*?color\s*?=\"?[^\"]?(#.*?)\"?>((.|\n)*?)<\/font><\/a>/i;
//var arr = re.exec(str); // 返回1个 用几次返回几次
//var arr = str.match(re); // 如果指定了参数 g,那么 match 一次返回所有的结果 String 对象的方法
var s="[";
var arr = re.exec(str);
s += "{\"id\":" + RegExp.$1 + ",\"c\":\"" + RegExp.$2 + "\",\"s\":\"" + RegExp.$3 + "\"}";
for (i = 2; arr != null; i++){
// RegExp.rightContext 返回被搜索的字符串中从最后一个匹配位置开始到字符串结尾之间的字符。只读。
// 此脚本会随着查找次数而将原始数据的调用减少.
var arr = re.exec(RegExp.rightContext);if(arr != null)
s += ",{\"id\":" + RegExp.$1 + ",\"c\":\"" + RegExp.$2 + "\",\"s\":\""+ RegExp.$3 + "\"}";
}
s+="]";
//alert(s);
var tempstr = eval(s);
//alert(tempstr[0].c);
//[{"s":1,"t":0,"id":13},{"s":1,"t":0,"id":12},{"s":1,"t":0,"id":11}] eval(s)
//{"s":1,"t":"23天20小时10分钟","u":20,"ser":"OBUCNZSPDXOTINZKEY10"} eval('('+s+')')
}
</script>
</HEAD>
<body onLoad="setData();">
<div id="div1">
<a href="qy.php?id=129"><font color="#000000">公司</font></a>
<a href="qy.php?id=130"><font color="#000000">有限</font></a>
<a href="qy.php?id=132"><font color="#000000">中法</font></a>
<a href="qy.php?id=129"><font color="#000000">北京</font></a>
<a href="qy.php?id=130"><font color="#000000">葡萄</font></a>
<a href="qy.php?id=132"><font color="#000000">王朝</font></a>
</div>
<br>
</body>
</HTML>
一个匹配多个的例子
代码
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>yelangshizhan</title>
<style type="text/css">
<!--
.css_news {
font-family: "宋体";
font-size: 12pt;
text-align: left;
}
.blog_title {
display:inline;
margin-right:10px;
}
.css_tbhome
{
font-size: 9pt;
text-align: left;
}
-->
</style>
<script language="javascript">
function GetCookie(name) {
var aCookie = document.cookie.split("; ");
for (var i=0; i < aCookie.length; i++) {
var aCrumb = aCookie[i].split("=");
if (name == aCrumb[0])
return unescape(aCrumb[1]);
}
return null;
}
function SetCookie(name,value) {
var date = new Date();
date.setFullYear(date.getFullYear() + 1, date.getMonth(), date.getDate());
document.cookie = name + "=" + escape(value) + "; expires=" + date.toGMTString();
}
function getXML(URL)
{
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.Open("GET",URL, false);
try
{
xmlhttp.send();
var result = xmlhttp.status;
}
catch(e)
{return(false);}
if(result==200)
{
return xmlhttp.responseText;
}
delete(xmlhttp)
}
var i = 0;
var count=0;
function setData()
{
/*
var nlen = GetCookie("nlen"); // 1
//野狼 utf-8
var newstr = getXML('http://blog.sina.com.cn/yelangshizhan');
var re = new RegExp("<div class=\"blog_title_h\">((.|\\n)*?)</span></div>","gi");
//var newstr = getXML('http://www.cnblogs.com/reommmm/admin/Feedback.aspx');
//var re = new RegExp("<td class=\"FeedfackTitle\">((.|\\n)*?)</td>","gi");
var a = newstr.match(re);
//alert(a.length);
//alert("GetCookie:" + nlen + " a.length:" + a.length);
if( nlen != a.length) // 2
{
SetCookie("nlen",a.length); //3
alert("update");
}
document.getElementById("news").innerHTML = "";
//for(var i=0;i<a.length;i++)
for(var i=0;i<a.length;i++)
document.getElementById("news").innerHTML += a[i] + "</br>";
setTimeout("setData()",4000);
//alert("野狼");
*/
document.getElementById("news").innerHTML = "抓取中....";
var str_rep = "";
var newstr = getXML('http://blog.sina.com.cn/yelangshizhan');
//var re = /<div\s*?[^\"]*?\"blog_title_h\"[^>]*?\>(.*?)<\/div><span\s*?[^>]*?>(.*?)<\/span>/i;
var re =/<div class=\"blog_title_h\">(.*?)<\/div>/;
//var re = /<div\s*?[^\"]*?\"blog_title\"[^>]*?\><a\s*?[^>]*?>(.*?)<\/a><\/div><span\s*?[^>]*?>(.*?)<\/span>/i;
var arr = re.exec(newstr);
//alert(RegExp.$1);
document.getElementById("news").innerHTML = RegExp.$1 + "-"+ RegExp.$2 +"</br>";
for(var i = 2 ; arr != null ;i++)
{
var arr = re.exec(RegExp.rightContext);
if(arr != null)
document.getElementById("news").innerHTML += RegExp.$1 + "-"+ RegExp.$2 + "</br>";
}
setTimeout("setData()",4000);
document.getElementById("tbhome").innerHTML = count++;
}
</script>
</HEAD>
<body onLoad="setData();">
<form id="frmHome" method="post">
<div align="center">
<div name="tbhome" id="tbhome" class="css_tbhome"></div>
<div name="news" id="news" class="css_news"></div>
</div>
</form>
</body>
</HTML>