<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<title>指令碼之家</title>
<script type="text/javascript">
varurl ="http://www.jb51.net/o.php?mod=viewthread&tid=14743&extra=page%3D1";
//第一參數為字串
console.group("字串");
varoneResult = url.replace("www.jb51.net",function(){
console.log("replace輸入參數:%o",arguments);
varval = /www.jb51.net/.exec(url);
console.log("exec輸出參數:%o",val);
console.assert(arguments[0] === val[0]);
console.assert(arguments[1] === val["index"]);
console.assert(arguments[2] === val["input"]);
return"jb51";
});
console.log("replace返回字串:"+oneResult);
console.groupEnd("字串");
//第一參數為Regex
console.group("Regex");
varregexp_global = /[?&](\w+)=([^&]*)/g;
varcount = 0;
vartwoResult = url.replace(regexp_global,function(){
console.log("第"+(count++)+"次運行");
console.log("replace輸入參數:%o",arguments);
varval = regexp_global.exec(url);
console.log("exec輸出參數:%o",val);
console.assert(arguments[0] === val[0]);
console.assert(arguments[1] === val[1]);
console.assert(arguments[2] === val[2]);
console.assert(arguments[3] === val["index"]);
console.assert(arguments[4] === val["input"]);
returncount;
});
console.log("replace返回字串:"+twoResult);
console.groupEnd("Regex");
</script>
</head>
<body>
</body>
</html>