Array element Lookup and substitution (JS indexof, etc.) in an array of JavaScript __java

Source: Internet
Author: User
Tags javascript array
<title> Extend JavaScript Array method </title>
<script language= "JavaScript" >
<!--
Array.prototype.indexof=function (Substr,start) {
var ta,rt,d= ';
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= ';
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= ' ";
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= ' ", 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 (' aa1321321 ', ' 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 with *
document.getElementById (' T5 '). Value=a.search (/dd4654/)//Not found return-1
};
-->
</script>
<style type= "Text/css" >
<!--
input{width:600px}
-->
</style>


<body>
<table>
<tr><td> array a:</td><td><input id= "T1" type= "text"/></td></tr>
<tr><td>a.indexof (' cc345345 '): </td><td><input id= "T2" type= "text"/></td>< /tr>
<tr><td>a.lastindexof (' dd4654 '): </td><td><input id= "T3" type= "text"/></td> </tr>
<tr><td>a.replace (/\d/g, ' * '): </td><td><input id= "T4" type= "text"/></td></ Tr>
<tr><td>a.search (/dd4654/): </td><td><input id= "T5" type= "text"/></td></tr >
</table>
</body>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.