使用indexOf等在JavaScript的數(shù)組中進(jìn)行元素查找和替換
更新時(shí)間:2013年09月18日 15:46:58 作者:
使用slice、replace、indexOf等等在JavaScript的數(shù)組中進(jìn)行元素的查找和替換,感興趣的朋友可以學(xué)習(xí)下
復(fù)制代碼 代碼如下:
<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('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','*');//把第一個(gè)b4323424b替換成*
document.getElementById('t5').value=a.search(/dd4654/);//未找到返回-1
};
//-->
</script>
<style type="text/css">
<!--
input{width:600px}
//-->
</style>
</head>
<body>
<table>
<tr><td>數(shù)組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>
</html>
您可能感興趣的文章:
- JS查找數(shù)組中重復(fù)元素的方法詳解
- 利用js查找數(shù)組中指定元素并返回該元素的所有索引示例
- javascript實(shí)現(xiàn)查找數(shù)組中最大值方法匯總
- JavaScript使用二分查找算法在數(shù)組中查找數(shù)據(jù)的方法
- javascript 折半查找字符在數(shù)組中的位置(有序列表)
- JavaScript實(shí)現(xiàn)在數(shù)組中查找不同順序排列的字符串
- js中數(shù)組結(jié)合字符串實(shí)現(xiàn)查找(屏蔽廣告判斷url等)
- JS算法題之查找數(shù)字在數(shù)組中的索引位置
- JavaScript遍歷查找數(shù)組中最大值與最小值的方法示例
- JS實(shí)現(xiàn)查找數(shù)組中對(duì)象的屬性值是否存在示例
相關(guān)文章
JavaScript HTML DOM元素 節(jié)點(diǎn)操作匯總
這篇文章主要介紹了JavaScript HTML DOM元素 節(jié)點(diǎn)操作匯總,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07微信小程序?qū)崿F(xiàn)發(fā)送短信驗(yàn)證碼倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)發(fā)送短信驗(yàn)證碼倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08JavaScript 新手24條實(shí)用建議[TUTS+]
本文列出了24條能讓你的代碼編寫過程更為輕松高效的建議。也許您還是JavaScript初學(xué)者,剛剛寫完自己的Hello World,那這里有很多對(duì)您的工作將十分有用的小貼士;也許有些技巧您已經(jīng)知道,那就試試快速瀏覽一下,看能不能發(fā)現(xiàn)一點(diǎn)新東西吧!2009-06-06JavaScript實(shí)現(xiàn)點(diǎn)擊復(fù)制功能具體代碼(JS訪問剪貼板相關(guān))
這篇文章主要給大家介紹了關(guān)于JavaScript實(shí)現(xiàn)點(diǎn)擊復(fù)制功能(JS訪問剪貼板相關(guān))的相關(guān)資料,復(fù)制功能指的是將一個(gè)文本或者圖片等資源從一個(gè)位置通過復(fù)制的方式再次拷貝到另一個(gè)位置,需要的朋友可以參考下2023-10-10canvas實(shí)現(xiàn)簡(jiǎn)易的圓環(huán)進(jìn)度條效果
本文主要分享了canvas實(shí)現(xiàn)簡(jiǎn)易的圓環(huán)進(jìn)度條效果的實(shí)例,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02微信小程序與axios組成網(wǎng)絡(luò)層封裝過程詳解
小程序在網(wǎng)絡(luò)層提供的API是能夠完成一個(gè)程序與服務(wù)端交互的完整鏈路,但需要大量的定制化代碼,才能實(shí)現(xiàn)請(qǐng)求攔截和響應(yīng)攔截,不太符合大多數(shù)開發(fā)者的使用習(xí)慣,對(duì)于前端開發(fā)者來(lái)說(shuō),網(wǎng)絡(luò)層還得是axios2023-02-02