Search and replace array elements in the JavaScript Array

Source: Internet
Author: User
Tags javascript array

Javascript provides the indexof (), lastindexof (), and search () functions for searching substrings in strings, and the Replace () function for strings (), these functions are not implemented in the array object array.

To enable array to support the above methods, I modified the array object prototype and added four functions. These four functions have the same name and syntax as the four corresponding functions of the string object. I will not introduce them much here.
See the example Code :
<HTML> <br/> <pead> <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/>}</P> <p> 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/>}</P> <p> 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/>}</P> <p> arr Ay. 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/>}</P> <p> window. onload = function () {<br/> var A = new array ('aa1338521', 'b4323424b', 'cc34 5345 ', 'dd4654', 'dd4654', 'b4323424b'); <br/> document. getelementbyid ('t1 '). value =. tostring (); <br/> document. getelementbyid ('t2 '). value =. indexof ('cc345345'); <br/> document. getelementbyid ('t3 '). value =. lastindexof ('dd4654'); <br/> document. getelementbyid ('t4 '). value =. replace (/\ D/g, '*'); <br/> document. getelementbyid ('t5 '). value =. search (/dd4654/); <br/>}; <br/> // --> <br/> </SCRIPT> <br/> <style type = "t EXT/CSS "> <br/> <! -- <Br/> input {width: 600px} <br/> // --> <br/> </style> <br/> </pead> </P> <p> <body> <br/> <Table> <br/> <tr> <TD> array: </TD> <input id = "T1" type = "text"/> </TD> </tr> <br/> <tr> <TD>. indexof ('cc345345 '): </TD> <input id = "T2" type = "text"/> </TD> </tr> <br/> <tr> <TD>. lastindexof ('dd4654 '): </TD> <input id = "T3" type = "text"/> </TD> </tr> <br/> <tr> <TD>. replace (/\ D/g ,'*'): </TD> <input id = "T4" type = "text"/> </TD> </tr> <br/> <tr> <TD>. search (/dd4654 /): </TD> <input id = "T5" type = "text"/> </TD> </tr> <br/> </table> <br/> </body> <br/> </ptml> <br/>

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.