<div class="codetitle"> <span><a style="CURSOR: pointer" data="4389" class="copybut" id="copybut4389" onclick="doCopy('code4389')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code4389"> <br> <br> <br><title> Extend JavaScript Array Method </title> <br><script language="javascript"> <BR><!-- <BR>Array.prototype.indexOf=function(substr,start){ <BR>var ta,rt,d='\0'; <BR>if(start!=null){ta=this.slice(start);rt=start;}else{ta=this;rt=0;} <BR>var str=d+ta.join(d)+d,t=str.indexOf(d+substr+d); <BR>if(t==-1)return -1;rt+=str.slice(0,t).replace(/[^\0]/g,'').length; <BR>return rt; <BR>} <br><br>Array.prototype.lastIndexOf=function(substr,start){ <BR>var ta,rt,d='\0'; <BR>if(start!=null){ta=this.slice(start);rt=start;}else{ta=this;rt=0;} <BR>ta=ta.reverse();var str=d+ta.join(d)+d,t=str.indexOf(d+substr+d); <BR>if(t==-1)return -1;rt+=str.slice(t).replace(/[^\0]/g,'').length-2; <BR>return rt; <BR>} <br><br>Array.prototype.replace=function(reg,rpby){ <BR>var ta=this.slice(0),d='\0'; <BR>var str=ta.join(d);str=str.replace(reg,rpby); <BR>return str.split(d); <BR>} <br><br>Array.prototype.search=function(reg){ <BR>var ta=this.slice(0),d='\0',str=d+ta.join(d)+d,regstr=reg.toString(); <BR>reg=new RegExp(regstr.replace(/\/((.|\n)+)\/.*/g,'\\0$1\\0'),regstr.slice(regstr.lastIndexOf('/')+1)); <BR>t=str.search(reg);if(t==-1)return -1;return str.slice(0,t).replace(/[^\0]/g,'').length; <BR>} <br><br>window.onload=function(){ <BR>var a=new Array('aa1321321','b4323424b','cc345345','dd4654','dd4654','b4323424b'); <BR>document.getElementById('t1').value=a.toString(); <BR>//document.getElementById('t2').value=a.indexOf('cc345345'); <BR>//document.getElementById('t3').value=a.lastIndexOf('dd4654'); <BR>document.getElementById('t4').value=a.replace('b4323424b','*');//把第一个b4323424b替换成* <BR>document.getElementById('t5').value=a.search(/dd4654/);//未找到返回-1 <BR>}; <BR>//--> <BR></script> <br><style type="text/css"> <BR><!-- <BR>input{width:600px} <BR>//--> <BR></style> <br> <br><br> <br><table> <br><tr> <td>数组a:</td> <td><input id="t1" type="text"></td> </tr> <br><tr> <td>a.indexOf('cc345345'):</td> <td><input id="t2" type="text"></td> </tr> <br><tr> <td>a.lastIndexOf('dd4654'):</td> <td><input id="t3" type="text"></td> </tr> <br><tr> <td>a.replace(/\d/g,'*'):</td> <td><input id="t4" type="text"></td> </tr> <br><tr> <td>a.search(/dd4654/):</td> <td><input id="t5" type="text"></td> </tr> <br> </table> <br> <br> <br> </div>