字符串::
string.prototype.trim = function(){返回this.replace(/^/s+|/s+$/g,“”); } string.prototype.ltrim = function(){return this.replace(/^/s+/g,“”); } string.prototype.rtrim = function(){返回this.replace(// s+$/g,“”); } string.prototype.splitandtrim = function($ deLimiter,$ limit){var $ ss = this.split($ deLimiter,$ limit);對於(var $ i = 0; $ i <$ ss.length; $ i ++)$ ss [$ i] = $ ss [$ i] .trim();返回$ ss; } string.prototype.htmlentities = function(){返回this.replace(/&/g,'&')。替換(/</g,'<')。替換(/>/>/>/g,'>'); } string.prototype.striptags = function(){return this.replace(/<([^>]+)>/g,'''); } string.prototype.toArray = function(){return this.split('''); } string.prototype.tointArray = function(){var returnArray = []; for(var i = 0; i <this.length; i ++){returnArray.push(this.charcodeat(i)); }返回returnArray; } string.prototype.replaceall = function($ old,$ snew){return this.replace(new Regexp($ old,“ gm”),$ snew); }變量替換
var a =“我愛{0},你愛{1},{0}!”; a.format(“ you”,“ me”); string.prototype.format = function(){var args = garmuments;返回this.replace(// {(/d+)/}/g,function(m,i,o,n){return args [i];}); }在字符串末尾追加字符串
String.prototype.append = function($ str){return this.concat($ str); }刪除指定索引位置的字符,索引無效將不刪除任何字符
string.protype.deletecharat = function($ sindex){if($ sindex <0 || $ sindex> = this.length){return this.valueof(); } else if($ sindex == 0){返回this.substring(1,this.length); } else if($ sindex == this.length-1){返回this.substring(0,this.length-1); } else {返回this.substring(0,$ sindex)+this.substring($ sindex+1); }}}刪除指定索引間的字符串.$sIndex和$eIndex所在的字符不被刪除!依賴deleteCharAt
string.protype.deletestring = function($ sindex,$ eindex){if($ sindex == $ eindex){return this.deletecharat($ sindex); } else {if($ sindex> $ eindex){var tindex = $ eindex; $ eindex = $ sindex; $ sindex = tindex; } if($ sindex <0)$ sindex = 0; if($ eindex> this.length-1)$ eindex = this.length-1;返回this.substring(0,$ sindex+1)+this.substring($ eindex,this.length); }}}檢查字符串是否以某個字符串(str)結尾
string.protype.endswith = function($ str){返回this.substr(this.length -$ str.length)== $ str; }檢查該字符串是否以某個字符串開始
string.protype.startswith = function(str){返回this.substr(0,str.length)== str; }比較兩個字符串是否相等,不區分大小寫!
string.protype.equalsignorecase = function($ str){if(this.length!= $ str.length){返回false; } else {var tmp1 = this.tolowercase(); var tmp2 = $ str.tolowercase();返回TMP1 == TMP2; }}}將指定的字符串插入到指定的位置後面!索引無效將直接追加到字符串的末尾
string.prototype.insert = function($ ofset,$ str){if($ ofset <0 || $ dourset> = this.length-1){return this.concat($ str)($ str); }返回this.substring(0,$ ofset)+$ str+this.substring($ ofset+1); }將指定的位置的字符設置為另外指定的字符或字符串。
string.protype.setCharat = function($ ofset,$ str){if($ ofset <0 || $ dourset> = this.length-1){return this.valueof(); }返回this.substring(0,$ ofset)+$ str+this.substring($ ofset+1); } string.prototype.replacelen = function(start,len,replated){if(!len)返回此;如果(start> = this.length)返回此; var returnseg =''; var returnseg2 =''; var i = 0; for(; i <this.length; i ++){var c = this.charat(i);如果(i <start)returnseg += c; if(i> = start + len)returnseg2 + = c; } return returnseg +替換 + returnseg2; }擴展基礎類:
替換字符,這個在替換填入比較有用,比如***天
string.prototype.replacechar = function(target,替換,啟動){if(!target)返回此;如果(!開始)開始= 0; var returnVal = this.substring(0,start); var index = 0; for(var i = start; i <this.length; i ++){var c = this.charat(i); target = typeof target =='函數'? target.call(this,index):target; if(c ==目標){returnVal += typeof替換=='function'?替換。 while(i <this.length -1 && this.charat(i+1)== c){i ++; } index ++; } else {returnVal += c; }} return returnVal; }將該字符串反序排列
string.prototype.reverse = function(){var str =“”; for(var i = this.length-1; i> = 0; i-){str = str.concat(this.charat(i)); }返回str; }計算長度,每個漢字佔兩個長度,英文字符每個佔一個長度
string.prototype.uclength = function(){var len = 0; for(var i = 0; i <this.length; i ++){if(this.charcodeat(i)> 255)len+= 2;否則len ++; }返回len; }在字符串的左邊填充一些特定的字符
string.prototype.lpad = function(len,s){var a = new array(this); var n =(len -this.length); for(var i = 0; i <n; i ++){a.unshift(s); }返回A.Join(“”); }在字符串的右邊填充一些特定的字符
string.prototype.rpad = function(len,s){var a = new array(this); var n =(len -this.length); for(var i = 0; i <n; i ++){a.push(s); }返回A.Join(“”); }把字符串的首字母轉化為大寫
string.protype.ucwords = function(){返回this.substring(0,1).touppercase()。 concat(this.substring(1)); } string.prototype.contains = function($ str){返回this.indexof($ str)> -1? true:false; }將格式為2008-04-02 10:08:44的字符串轉成日期(字符串:2008-04-02 10:08:44)
string.prototype.todate = function(){var str = this.replace(/ - /g,“/”);返回(新日期(str)); }將原來用字符串表示的十進數轉成十進制浮點數:精度為精度
string.protype.tofloat = function(precision){precision = precision || 2;返回parsefloat(this,10).tofixed(precision); }將原來用字符串表示的十進數轉成十進制整數
string.prototype.toint = function(){return parseint(this,10).toString(); }將兩個原來用字符串表示的十進數相加後當作字串返回:加成為加數
string.prototype.add = function(Addend){var sum = parsefloat(this,10) + parsefloat(Addend,10);返回sum+“”; }sextscale為進制為進制為進制為進制2,8,16
string.protype.shiftscale = function(nextScale){return parsefloat(this).toString(nextScale); }各進制互相轉換:
這個
@param預賽原是是幾進制數
@Param NextScale要轉換成幾進制數
string.prototype.scaleshift = function(預刻,nextscale){return parseint(this,prescale).toString(nextScale); }全角2半角document.write(“ abc 123,我們都是好朋友”);
string.prototype.dbc2sbc = function(){
返回this.replace(/[/uff01-/uff5e]/g,function(a){return string.fromcharcode(a.charcodeat(0)-65248);})。
}
陣列擴展函數:
var isnumeric = function(x){//如果x是數字而false,則返回true。 var regexp =/^(-)? (/d*):(/ .?)/d* facte返回字符串(x).match(regexp); 。 var Oddarray = myArray.filter(isnumeric); //輸出:1,3,5,7,9 var Oddarray = myArray.ysome(iSnumeric); //輸出:true var Oddarray = myArray.every(isnumeric); //輸出:false var printarray = function(x,idx){document.writeln('['+idx+'] ='+x); } myArray.foreach(printArray); //輸出:[0] = 1 [1] =兩個[2] = 3 [3] =四[4] = 5 myArray.remove(9); document.writeln(myarray);如果(!array.prototype.every){array.prototype.every = function(fun /*,thisp* /){var len = this.length; if(typeof fun!=“ function”)拋出新的typeError(); var thisp =參數[1]; for(var i = 0; i <len; i ++){if(i在此&&! }返回true; }; } if(!array.prototype.filter){array.prototype.filter = function(fun /*,thisp* /){var len = this.length; if(typeof fun!=“ function”)拋出新的typeError(); var res = new array(); var thisp =參數[1]; for(var i = 0; i <len; i ++){if(i在此){var val = this [i]; //如果有趣的話,如果(fun.call(thisp,val,i,this))res.push(val); }}返回res; }; } if(!array.prototype.foreach){array.prototype.foreach = function(fun /*,thisp* /){var len = this.length; if(typeof fun!=“ function”)拋出新的typeError(); var thisp =參數[1]; for(var i = 0; i <len; i ++){if(i在此)fun.call(thisp,this [i],i,this); }}; } if(!array.prototype.map){array.prototype.map = function(fun /*,thisp* /){var len = this.length; if(typeof fun!=“ function”)拋出新的typeError(); var res = new Array(Len); var thisp =參數[1]; for(var i = 0; i <len; i ++){if(i在此)res [i] = fun.call(thisp,this [i],i,i,this); }返回res; }; } if(!array.prototype.some){array.prototype.some = function(fun /*,thisp* /){var len = this.length; if(typeof fun!=“ function”)拋出新的typeError(); var thisp =參數[1]; for(var i = 0; i <len; i ++){if(i在此&& fun.call中(thisp,this [i],i,i,this))返回true; }返回false; }; } array.prototype.sortnum = function(){return this.sort(function(a,b){return ab;}); } <! - var tmp = [5,9,12,18,56,1,10,42,'Blue',30,7,97,53,33,33,33,33,35,35,35,30,'35','35','Ball','Ball','Bubble']; var thirty = tmp.find(30); //返回9、14、17 var thirtyfive = tmp.find('35'); //返回18 var thirtyfive = tmp.find(35); //返回15 var haveblue = tmp.find('blue'); //返回8 var notFound = tmp.find('not!'); //返回false var regexp1 = tmp.find(/^b/); //返回8,20(首字母以b)var regexp1 = tmp.find(/^b/i); //返回8,19,20(與上述相同,但忽略案例) - > array.prototype.find = function(searchstr){var returnarray = false; for(i = 0; i <this.length; i ++){if(typeof(searchstr)=='function'){if(searchstr.test(this [i]))){if(!returnArray){returnArray){returnArray = []} returnArray = [] returnArray.push(i); }} else {if(this [i] === searchstr){if(!returnArray){returnArray = []} returnArray.push(i); }}} return returnArray; }隨機改變數組的排序
array.prototype.shuffle = function(){for(var rnd,tmp,i = this.length; i; rnd = parseint(math.random()*i),tmp = this [ - i],this [i] = this [i] = this [rnd],this [rnd],this [rnd] = tmp);返回此; } <! - var myarray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]; var YourArray = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]; document.writeln(myArray.compare(yourArray)); //輸出:true; - > array.prototype.compare = function(testarr){if(this.length!= testarr.length)返回false; for(var i = 0; i <testarr.length; i ++){if(this [i] .compare){if(!this [i] .compare(testarr [i]))返回false; } if(this [i]!== testarr [i])返回false; }返回true; }去掉數組中重複的值var a = new Array(“ 5”,“ 7”,“ 7”); a.unique();
array.prototype.unique = function(){var data = this || []; var a = {}; //聲明一個對象,javascript的對象可以當哈希表用for(var i = 0; i <data.length; i ++){a [data [i]] = true; //設置標記,把數組的值當下標,這樣就可以去掉重複的值} data.length = 0;對於(a)中的(var i){//遍歷對象,把已標記的還原成數組,this [data.length] = i; }返回數據; } array.prototype.addall = function($ array){if($ array == null || $ array.length == 0)return;對於(var $ i = 0; $ i <$ array.length; $ i ++)this.push($ array [$ i]); } array.protype.contains = function($ value){for(var $ i = 0; $ i <this.length; $ i ++){var $ element = this [$ i];如果($ element == $ value)返回true; }返回false; } array.protype.indexof = function($ value){for(var $ i = 0; $ i <this.length; $ i ++){if(this [$ i] == $ value)返回$ i; }返回-1; } if(!array.prototype.lastIndexof){array.prototype.lastIndexof = function(elt /*,來自* /){var len = this.length; var fules = number(gragments [1]); if(isnan(來自)){from = len -1; } else {from =(來自<0)? Math.ceil(來自):Math.floor(從); if(從<0)來自 += len;否則if(來自> = len)來自= len -1; } for(; from> -1; from--){if(在此&& this [來自] === ELT中)從; }返回-1; }; } array.prototype.intertat = function($ value,$ index){if($ index <0)this.unshift($ value);否則if($ index> = this.length)this.push($ value);否則這個splice($ index,0,$ value); }根據數組的下標來刪除元素
array.prototype.removebyIndex = function($ n){if($ n <0){//如果n <0,則不進行任何操作。返回; } else {返回this.slice(0,$ n).concat(this.slice($ n+1,this.length)); }}}依賴索引
array.prototype.remove = function($ value){var $ index = this.indexof($ value); if($ index!= -1)this.splice($ index,1); } array.prototype.removeall = function(){while(this.length> 0)this.pop(); } array.protype.replace = function($ oldvalue,$ newValue){for(var $ i = 0; $ i = 0; $ i <this.length; $ i ++){if(this [$ i] == $ oldValue){this [$ i] = $ newValue;返回; }}} array.prototype.swap = function($ a,$ b){if($ a == $ b)返回; var $ tmp = this [$ a];這個[$ a] = this [$ b];這個[$ b] = $ tmp; } array.prototype.max = function(){return Math.max.apply({},this); } array.prototype.min = function(){return math.min.apply({},this); } array.protype.splice = function(start,dellen,item){var len = this.length; start = start <0?0:start> len? len:start? start:0; dellen = dellen <0?0:dellen> len? len:dellen? dellen:len; var arr = [],res = []; var iarr = 0,ires = 0,i = 0; for(i = 0; i <len; i ++){if(i <start || ires> = dellen)arr [iarr ++] = this [i]; else {res [ires ++] = this [i]; if(item && ires == dellen){arr [iarr ++] = item; }}} if(item && ires <dellen)arr [iarr] = item; for(var i = 0; i <arr.length; i ++){this [i] = arr [i]; } this.length = arr.length;返回res; } array.protype.shift = function(){if(!this)return []; return this.splice(0,1)[0];}分開添加,關鍵字淺副本,如果遇到數組,複製數組中的元素
array.prototype.concat = function(){var i = 0; while(i <gragments.length){if(typeof gragonments [i] ==='對象'&& typeof參數[i]。 var j = 0; while(j <gragments [i] .length)this.splice(this.length,0,參數[i] [j ++]); i ++; } else {this [this.length] = gragments [i ++]; }}返回此; } array.prototype.join = function(saparator){var i = 0,str =“”; while(i <this.length)str+= this [i ++]+saparator;返回str; } array.protype.pop = function(){return this.splice(this.length-1,1)[0];} array.prototype.push.push = function(){array.prototype.splice.apply(this,[this,[this.length,0] .concat(aray.protototype.ssplice.splice.splysapply(griments)) //這裡沒有直接處理參數,而是複制了一下返回this.length; } array.protype.reverse = function(){for(var i = 0; i <this.length/2; i ++){var temp = this [i];這個[i] = this [this.length-1-i];這個[this.length-1-i] = temp; }返回此; } array.prototype.slice = function(start,end){var len = this.length; start = start <0?開始+= len:start?start:0; end = end <0?end+= len:end> len?len:end?end?end:len; var i = start; var res = []; while(i <end){res.push(this [i ++]); }返回res; } //arr.unshift(ele1,ele2 ,, ele3 ....)array.prototype.unshift = function(){array.prototype.splice.apply(this,[0,0,0] .concat(array.prototype.slice.slice.slice.slice.slice.apply.apply(this,gruments))); }