Copy codeThe Code is as follows:
<Html>
<Head>
<Title> Extend JavaScript Array Method </title>
<Script language = "javascript">
<! --
Array. prototype. indexOf = function (substr, start ){
Var ta, rt, d = '\ 0 ';
If (start! = Null) {ta = this. slice (start); rt = start;} else {ta = this; rt = 0 ;}
Var str = d + ta. join (d) + d, t = str. indexOf (d + substr + d );
If (t =-1) return-1; rt + = str. slice (0, t ). replace (/[^ \ 0]/g ,''). length;
Return rt;
}
Array. prototype. lastIndexOf = function (substr, start ){
Var ta, rt, d = '\ 0 ';
If (start! = Null) {ta = this. slice (start); rt = start;} else {ta = this; rt = 0 ;}
Ta = ta. reverse (); var str = d + ta. join (d) + d, t = str. indexOf (d + substr + d );
If (t =-1) return-1; rt + = str. slice (t ). replace (/[^ \ 0]/g ,''). length-2;
Return rt;
}
Array. prototype. replace = function (reg, rpby ){
Var ta = this. slice (0), d = '\ 0 ';
Var str = ta. join (d); str = str. replace (reg, rpby );
Return str. split (d );
}
Array. prototype. search = function (reg ){
Var ta = this. slice (0), d = '\ 0', str = d + ta. join (d) + d, regstr = reg. toString ();
Reg = new RegExp (regstr. replace (/\/((. | \ n) + )\/. */g, '\ 0 $1 \ 0'), regstr. slice (regstr. lastIndexOf ('/') + 1 ));
T = str. search (reg); if (t =-1) return-1; return str. slice (0, t ). replace (/[^ \ 0]/g ,''). length;
}
Window. onload = function (){
Var a = new Array ('aa1338521 ', 'b4323424b', 'cc345345', 'dd4654', 'dd4654', 'b4323424b ');
Document. getElementById ('t1'). value = a. toString ();
// Document. getElementById ('t2'). value = a. indexOf ('cc345345 ');
// Document. getElementById ('t3 '). value = a. lastIndexOf ('dd4654 ');
Document. getElementById ('t4 '). value = a. replace ('b4323424b',' * '); // replace the first b4323424b *
Document. getElementById ('t5 '). value = a. search (/dd4654/); // No-1 is found
};
// -->
</Script>
<Style type = "text/css">
<! --
Input {width: 600px}
// -->
</Style>
</Head>
<Body>
<Table>
<Tr> <td> array a: </td> <input id = "t1" type = "text"/> </td> </tr>
<Tr> <td>. indexOf ('cc345345'): </td> <input id = "t2" type = "text"/> </td> </tr>
<Tr> <td>. lastIndexOf ('dd4654'): </td> <input id = "t3" type = "text"/> </td> </tr>
<Tr> <td>. replace (/\ d/g ,'*'): </td> <input id = "t4" type = "text"/> </td> </tr>
<Tr> <td>. search (/dd4654/): </td> <input id = "t5" type = "text"/> </td> </tr>
</Table>
</Body>
</Html>