正则表达式替换 RegExp replace
<!DOCTYPE html>
<html>
<body>
<a href="http://www.taobao.com/index_global.php?pid=mm_0_0_0">0000</a>
<a href="http://www.taobao.com/index_global.php?pid=mm_123_0_0">111</a>
<a href="http://www.taobao.com/index_global.php?pid=mm_123_0_0">2222</a>
<a href="http://www.taobao.com/index_global.php?pid=mm_888_999_0000">33333</a>
<script type="text/javascript">
(function(){
var isrun = false;
var init_pid = function(){
if(isrun) return;
var str=window.location.search;
var pid= str.match(/pid=mm_d{0,10}_d{0,10}_d{0,10}/i);
if(pid){
pid = pid[0].split("=")[1];
var unid= str.match(/unid=d{0,10}/i);
if(unid){pid += "&"+unid[0]; }
var n = str.match(/n=d{0,5}/i);
if(n){pid += "&"+n[0]; }
//var reg=new RegExp("mm_0_0_0","gmi");
var reg = new RegExp(/mm_d{0,10}_d{0,10}_d{0,10}/ig);
var as = document.getElementsByTagName("A");
for(var i = 0; i < as.length; i++){
as[i].href=as[i].href.replace(reg,pid);
}
}
isrun = true;
}
setTimeout(init_pid,3000);
})();
</script>
</body>
</html>
出处:http://www.heiniuhaha.cn