文字列对象的扩展函数:
string.prototype.trim = function(){return this.replace(/^/s+|/s+$/g、 ""); } string.prototype.ltrim = function(){return this.replace(/^/s+/g、 ""); } string.prototype.rtrim = function(){return this.replace(// s+$/g、 ""); } string.prototype.splitandtrim = function($ delimiter、$ limit){var $ ss = this.split($ delimiter、$ limit); for(var $ i = 0; $ i <$ ss.length; $ i ++)$ ss [$ i] = $ ss [$ i] .trim(); $ ssを返します。 } string.prototype.htmlentities = function(){return this.replace(/&/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)); } Return 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 = arguments; this.replace(// {(/d+)/}/g、function(m、i、o、n){return args [i];}); }在字符串末尾追加字符串
string.prototype.append = function($ str){return this.concat($ str); }删除指定索引位置的字符、索引无效将不删除任何字符
string.prototype.deletecharat = function($ sindex){if($ sindex <0 || $ sindex> = this.length){return this.valueof(); } else if($ sindex == 0){return this.substring(1、this.length); } else if($ sindex == this.length-1){return this.substring(0、this.length-1); } else {return this.substring(0、$ sindex)+this.substring($ sindex+1); }}删除指定索引间的字符串。$sindex和$ eindex
string.prototype.deletestring = function($ sindex、$ eindex){if($ sindex == $ eindex){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.prototype.endswith = function($ str){return this.substr(this.length- $ str.length)== $ str; }检查该字符串是否以某个字符串开始
string.prototype.startswith = function(str){return this.substr(0、str.length)== str; }比较两个字符串是否相等、不区分大小写!
string.prototype.equalsignorecase = function($ str){if(this.length!= $ str.length){return false; } else {var tmp1 = this.tolowercase(); var tmp2 = $ str.tolowercase(); tmp1 == tmp2を返します。 }}将指定的字符串插入到指定的位置后面!索引无效将直接追加到字符串的末尾
string.prototype.insert = function($ ofset、$ str){if($ ofset <0 || $ ofset> = this.length-1){return this.concat($ str); } this.substring(0、$ ofset)+$ str+this.substring($ ofset+1); }将指定的位置的字符设置为另外指定的字符或字符串。索引无效将直接返回不做任何处理!
string.prototype.setcharat = function($ ofset、$ str){if($ ofset <0 || $ ofset> = this.length-1){return this.valueof(); } this.substring(0、$ ofset)+$ str+this.substring($ ofset+1); } string.prototype.Replacelen = function(start、len、chepladed){if(!len)return this; if(start> = this.length)thisを返します。 var returnseg = ''; var returnseg2 = ''; var i = 0; for(; i <this.length; i ++){var c = this.charat(i); if(i <start)returnseg += c; if(i> = start + len)returnseg2 + = c; } return returnseg +交換 + returnseg2; }扩展基础类:
替换字符、这个在替换填入比较有用、比如***天***小时<input />> <input />小时
string.prototype.replacechar = function(ターゲット、交換、start){if(!target)return this; if(!start)start = 0; var returnval = this.substring(0、start); var index = 0; for(var i = start; i <this.length; i ++){var c = this.charat(i);ターゲット= typeof target == 'function'? Target.Call(this、index):target; if(c ==ターゲット){returnval += typeof fluction '? cheded.call(this、index):置換; while(i <this.length -1 && this.charat(i+1)== c){i ++; } index ++; } else {returnval += c; }} returnval; }将该字符串反序排列
string.prototype.reverse = function(){var str = ""; for(var i = this.length-1; i> = 0; i - ){str = str.concat(this.charat(i)); } return str; }计算长度、每个汉字占两个长度、英文字符每个占一个长度
string.prototype.uclength = function(){var len = 0; for(var i = 0; i <this.length; i ++){if(this.charcodeat(i)> 255)len+= 2; else 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); } return 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); } return a.join( ""); }把字符串的首字母转化为大写
string.prototype.ucwords = function(){this.substring(0,1).touppercase()。concat(this.substring(1)); } string.prototype.contains = function($ str){return this.indexof($ str)> -1? True:false; }将格式为2008-04-0210:08:44
string.prototype.todate = function(){var str = this.replace(/ - /g、 "/"); return(new Date(str)); }将原来用字符串表示的十进数转成十进制浮点数:精度为精度
string.prototype.tofloat = function(precision){precision = precision || 2; parsefloat(this、10).tofixed(precision); }将原来用字符串表示的十进数转成十进制整数
string.prototype.toint = function(){return parseint(this、10).toString(); }将两个原来用字符串表示的十进数相加后当作字串返回:addend为加数
string.prototype.add = function(addend){var sum = parsefloat(this、10) + parsefloat(addend、10); return sum+""; }十进制转其他进制代码如下次のスケール为进制2,8,16
string.prototype.shiftscale = function(nextscale){return parsefloat(this).tostring(nextscale); }各进制互相转换:
これ对象必须是整数
@Param Prescale原是是几进制数
@Param NextScale要转换成几进制数
string.prototype.scaleshift = function(prescale、nextscale){return parseint(this、prescale).tosttring(nextscale); }全角2半角document.write( "abc 123、我们都是好朋友");
string.prototype.dbc2sbc = function(){
return this.replace(/[/uff01-/uff5e]/g、function(a){return string.fromcharcode(a.charcodeat(0)-65248);})。
}
配列扩展函数:
var isnumeric = function(x){// xが数値の場合はtrue、そうでない場合はfalseを返します。 var regexp = /^(-)?(/d*)(/.?)(/d*)$/; return string(x).match(regexp); } var myArray = [1、 '2'、3、 '4'、5、 '6'、7、 '8'、9、 'Ten']; var Oddarray = myarray.filter(isNumeric); //出力:1,3,5,7,9 var Oddarray = myArray.some(isnumeric); // outputs:True var Oddarray = myArray.ETERY(isNumeric); // outputs:false var printarray = function(x、idx){document.writeln( '['+idx+'] ='+x); } myarray.foreach(printArray); // outputs:[0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = 5 myArray.remove(9); document.writeln(myarray); if(!array.prototype.Every){array.prototype.every = function(fun /*、thisp* /){var len = this.length; if(typeof fun!= "function")throw new TypeError(); var thisp = arguments [1]; for(var i = 0; i <len; i ++){if(i in this &&!fun.call(this [i]、i、this))return false; } trueを返します。 }; } if(!array.prototype.filter){array.prototype.filter = function(fun /*、thisp* /){var len = this.length; if(typeof fun!= "function")throw new TypeError(); var res = new Array(); var thisp = arguments [1]; for(var i = 0; i <len; i ++){if(i in this){var val = this [i]; // Funがこれを変異させる場合は、if(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")throw new TypeError(); var thisp = arguments [1]; for(var i = 0; i <len; i ++){if(i in this)fun.call(this [i]、i、this); }}; } if(!array.prototype.map){array.prototype.map = function(fun /*、thisp* /){var len = this.length; if(typeof fun!= "function")throw new TypeError(); var res = new Array(len); var thisp = arguments [1]; for(var i = 0; i <len; i ++){if(i in this)res [i] = fun.call(this [i]、i、this); } RESを返します。 }; } if(!array.prototype.some){array.prototype.some = function(fun /*、thisp* /){var len = this.length; if(typeof fun!= "function")throw new TypeError(); var thisp = arguments [1]; for(var i = 0; i <len; i ++){if(i in this && fun.call(this [i]、i、this))return 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,30,35,27,30、'35 '、' 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 there!')を返します。 // 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.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 [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)); // outputs:true; - > array.prototype.compare = function(testarr){if(this.length!= testarr.length)return false; for(var i = 0; i <testarr.length; i ++){if(this [i] .compare){if(!this [i] .compare(testarr [i]))return false; } if(this [i]!== testarr [i])false; } trueを返します。 }var 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; for(var i in a){//遍历对象、把已标记的还原成数组this [data.length] = i; }データを返します。 } array.prototype.addall = function($ array){if($ array == null || $ array.length == 0)return; for(var $ i = 0; $ i <$ array.length; $ i ++)this.push($ array [$ i]); } array.prototype.contains = function($ value){for(var $ i = 0; $ i <this.length; $ i ++){var $ element = this [$ i]; if($ element == $ value)trueを返します。 } falseを返します。 } array.prototype.indexof = function($ value){for(var $ i = 0; $ i <this.length; $ i ++){if(this [$ i] == $ value)return $ i; } return -1; } if(!array.prototype.lastindexof){array.prototype.lastindexof = function(elt /*、from* /){var len = this.length; var from = number(arguments [1]); if(isnan(from)){from = len -1; } else {from =(from <0)? math.ceil(from):math.floor(from); if(<0から) += len; else(from> = len)from = len -1; } for(; from> -1; from-){if(this from this [from] === elt)return; } return -1; }; } array.prototype.insertat = function($ value、$ index){if($ index <0)this.unshift($ value); else if($ index> = this.length)this.push($ value); else this.splice($ index、0、$ value); }根据数组的下标来删除元素
array.prototype.removebyindex = function($ n){if($ n <0){// } else {return 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.prototype.replace = function($ oldvalue、$ newValue){for(var $ i = 0; $ i <this.length; $ i ++){if(this [$ i] == $ oldValue){this [$ i] = $ newValue;戻る; }}} array.prototype.swap = function($ a、$ b){if($ a == $ b)return; var $ tmp = this [$ a]; this [$ a] = this [$ b];この[$ b] = $ tmp; } array.prototype.max = function(){return math.max.apply({}、this); } array.prototype.min = function(){return math.min.apply({}、this); } array.prototype.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.prototype.shift = function(){if(!this)return []; return this.splice(0,1)[0];}分开添加、关键字、浅いコピー、如果遇到数组、复制数组中的元素
array.prototype.concat = function(){var i = 0; while(i <arguments.length){if(typeof arguments [i] === 'object' && typeof arguments [i] .splice === 'function' &&!arguments [i] .propertyisenumerable( 'length')){// shallow copy not shallowコピー以下// array.concat.apply [i++]; var j = 0; while(j <arguments [i] .length)this.splice(this.length、0、arguments [i] [j ++]); i ++; } else {this [this.length] = arguments [i ++]; }}これを返します。 } array.prototype.join = function(separator){var i = 0、str = ""; while(i <this.length)str+= this [i ++]+セパレーター。 strを返します。 } array.prototype.pop = function(){this.splice(this.length-1)[0];} array.prototype.push = function(){array.prototype.splice.apply(this、[this、length、0] .concat(array.slice.slice.splice.splice.splote.sply.slice.slice.slice.slice.slice.slice.slice.slice.slice.slice.slice.slice.slice.slice.slice。 //这里没有直接处理参数、而是复制了一下reture.length; } array.prototype.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?start+= len:start?start:0; end = end <0?end+= len:end> len?len:end?end:len; var i = start; var res = []; while(i <end){res.push(this [i ++]); } RESを返します。 } //arr.unshift(ELE1,LE2,LE3 ....)array.prototype.unshift = function(){array.prototype.splice.apply(this、[0,0] .concat(array.prototype.slice.apply(this、arguments))); }