<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>擴充JavaScript 陣列方法 <br> <br><!-- <BR>Array.prototype.indexOf=function(substr,start){ <BR>var ta,rt, d=' '; <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(/[^ ]/ g,'').length; <BR>返回rt; <BR>} <br><br>Array.prototype.lastIndexOf=function(substr,start){ <BR>var ta,rt,d=' '; <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(/[^ ] /g,'').length-2; <BR>返回rt; <BR>} <br><br>Array.prototype.replace=function(reg,rpby){ <BR>var ta=this.slice( 0),d=' '; <BR>var str=ta.join(d);str=str.replace(reg,rpby); <BR>回傳str.split(d); <BR>} <br> <br>Array.prototype.search=function(reg){ <BR>var ta=this.slice(0),d=' ',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(/[^ ]/g,''). length; <BR>} <br><br>window.onload=function(){ <BR>var a=new Array('aa1321321','b4323424b','cc345345','4654','dd4654','cc345345','4654','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>腳本> <br> <br><!-- <BR>輸入{寬度:600px} <BR>//--> <br>風格> <br>頭> <br><br> <br>; <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>表> <br>身體> <br> <br> </div>