资源描述:
《javascript常用字符串与数组扩展函数小结_基础知识》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、JavaScript常用字符串与数组扩展函数小结String对象的扩展函数:String.prototype,trim=function(){returnthis,replace(厂s+
2、s+$/g,〃“);}String.prototype.Itrim二function(){returnthis.replace(/"s+/g,“〃);}String,prototype,rtrim=function(){returnthis.replace(/s+$/g,〃〃);}String.prototypc.splitAndTrim二func
3、tion($dclimitcr,$limit){var$ss=this.split($delimiter,$limit);for(var$i=0;$i<$ss.length;$i++)$ss[$i]二$ss[$i].trimO;return$ss;}String.prototype.htmlEntities二function(){returnthis.replace(/&/g,'&')•replace(//g,';ijString.prototype.stripTags=function(){retur
4、nthis,replace(/〈(「>]+)>/g,'');}String.prototype.toArray二function(){returnthis,split(厂);}String,prototype.toIntArray=function(){varreturnArray二[];for(vari二0;i〈this.length;i++){returnArray.push(this.charCodcAt(i));}returnreturnArray;}String.prototype.replace"1=function($old,
5、$snew){returnthis,replace(newRegExp($old,〃gnT),$sncw);变量替换vara=Love{0},andYouLove{1},Whereare{0}!〃;a.format(z,Youz,,"Me");String.prototype,format二function(){varargs=argumcnts;returnthis,replace(/{(d+)}/g,function(m,i,o,n){returnargs[i];});}在字符串末尾追加字符串String.prototype.ap
6、pend二function($str){returnthis.concat($str);}删除指定索引位置的字符,索引无效将不删除任何字符String,prototype.deleteCharAt二function($slndex){if($sTndex<0
7、
8、$sTndex>=this.length){returnthis.valucOf();}elseif($slndex==0){returnthis.substring(1,this,length);}elseif($slndex二二this,length~l){returnthis.
9、substring(0,this.lengthT);}else{returnthis.substring(0,$slndex)+this.substring($slndex+l);}}删除指定索引间的字符串.SsTndex和SeTndex所在的字符不被删除!依赖deleteCharAtString.prototype.deleteString二function($slndex,$elndex){if($slndex==$elndex){returnthis.deleteCharAt($slndex);}else{if($slndex>$el
10、ndex){vartlndex=$elndex;$elndex=$slndex;$slndex=tlndex;}if($sTndex<0)$sIndex=O;if($cIndcx>this.lcngth-l)$cIndcx二this,lcngthT;returnthis.substring(O,$slndex+l)+this.substring($elndex,this,length);}}检查字符申是否以某个字符申(str)结尾String.prototype.endsWith=function($str){returnthis.subs
11、tr(this,length-$str.length)二二$str;}检查该字符串是否以某个字符串开始String,prototype.startsWith=function(s