String 对象的扩展函数:
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 = função ($ delimiter, $ limite) {var $ ss = this.split ($ delimiter, $ limite); para (var $ i = 0; $ i <$ ss.length; $ i ++) $ ss [$ i] = $ ss [$ i] .Trim (); retornar $ ss; } String.prototype.htmlentities = function () {return this.replace (/&/g, '&'). Substituir (/</g, '<'). Substituir (/>/g, '>'); } String.prototype.striptags = function () {return this.replace (/<([^>]+)>/g, ''); } String.prototype.toArray = function () {return this.split (''); } String.prototype.tointArray = function () {var returrray = []; for (var i = 0; i <this.length; i ++) {returnArray.push (this.charcodeat (i)); } return ReturnArray; } String.prototype.replaceall = function ($ antigo, $ snew) {return this.replace (novo regexp ($ antigo, "gm"), $ snew); }变量替换
var a = "eu amo {0}, e você ama {1}, onde estão {0}!"; A.Format ("você", "me"); String.prototype.format = function () {var args = argumentos; retornar este.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 所在的字符不被删除!依赖 DeleteCharat
String.prototype.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; Retorne this.substring (0, $ sindex+1)+this.substring ($ eiindex, 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 (); return tmp1 == tmp2; }}将指定的字符串插入到指定的位置后面! 索引无效将直接追加到字符串的末尾
String.prototype.insert = function ($ ofset, $ str) {if ($ ofset <0 || $ ofset> = this.Length-1) {return this.Concat ($ str); } Retorne this.substring (0, $ ofset)+$ str+this.substring ($ ofset+1); }将指定的位置的字符设置为另外指定的字符或字符串. 索引无效将直接返回不做任何处理!
String.prototype.Setcharat = function ($ OFSET, $ str) {if ($ ofSt <0 || $ ofSt> = this.length-1) {return this.valueof (); } Retorne this.substring (0, $ ofset)+$ str+this.substring ($ ofset+1); } String.prototype.replacelen = function (start, len, substituído) {if (! Len) retorne isso; if (start> = this.length) retorne isso; var returnSeg = ''; var returnSeg2 = ''; var i = 0; para (; i <this.length; i ++) {var c = this.charat (i); if (i <start) returnSeg += c; if (i> = start + len) returnSeg2 + = c; } returnseg + substituído + returnSeg2; }扩展基础类
替换字符 , 这个在替换填入比较有用 , 比如 *** 天 *** 小时 替换为 <input /> 天 <input /> 小时
String.prototype.replacechar = function (destino, substituído, start) {if (! Target) retorne isso; if (! Start) Start = 0; var returnVal = this.substring (0, start); var índice = 0; for (var i = start; i <this.length; i ++) {var c = this.charat (i); Target = TypeOf Target == 'Função'? Target.Call (isto, índice): Target; if (c == Target) {returnVal += typeOf substituído == 'função'? substituído.call (isto, índice): substituído; while (i <this.length - 1 && this.charat (i+1) == c) {i ++; } index ++; } else {returnVal += c; }} retornar retornarVal; }将该字符串反序排列
String.prototype.Vevers = function () {var str = ""; for (var i = this.length-1; i> = 0; i-) {str = str.Concat (this.charat (i)); } retornar 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 ++; } retornar 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); } retornar 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); } retornar A.Join (""); }把字符串的首字母转化为大写
String.prototype.ucwords = function () {return this.substring (0,1) .touppercase (). Concat (this.substring (1)); } String.prototype.contains = function ($ str) {return this.indexof ($ str)> -1? Verdadeiro: falso; }将格式为 2008-04-02 10:08:44 的字符串转成日期 (String 对象的值必须为: 2008-04-02 10:08:44)
String.prototype.todate = function () {var str = this.replace (/-/g, "/"); retornar (nova data (str)); }将原来用字符串表示的十进数转成十进制浮点数: Precisão 为精度
String.prototype.tofloat = function (precisão) {precisão = precisão || 2; retornar parsefloat (this, 10) .tofixado (precisão); }将原来用字符串表示的十进数转成十进制整数
String.prototype.toint = function () {return parseint (this, 10) .toString (); }将两个原来用字符串表示的十进数相加后当作字串返回: Adendo 为加数
String.prototype.add = function (adesão) {var sum = parsefloat (this, 10) + parsefloat (adesão, 10); soma de retorno+""; }十进制转其他进制代码如下 Próxima escala 为进制 如 2,8,16
String.prototype.shiftscale = function (nextscale) {return parsefloat (this) .toString (nextscale); }各进制互相转换
Este 对象必须是整数
@param pré -escala 原是是几进制数
@param nextscale 要转换成几进制数
String.prototype.scaleshift = function (prescale, nextscale) {return parseint (this, prescale) .ToString (nextscale); }全角 2 半角 document.write ("abc 123 , 我们都是好朋友");
String.prototype.dbc2sbc = function () {
Retorne this.Replace (/[/uff01-/uff5e]/g, function (a) {return string.fromcharcode (a.charcodeat (0) -65248);}). substituir (// u3000/g, "");
}
Array 扩展函数:
var isnumeric = function (x) {// retorna true se x for numérico e falso se não for. var regexp = /^(-)?(/d*)(/.?)(/d*)$/; return string (x) .match (regexp); } var myarray = [1, 'dois', 3, 'quatro', 5, 'seis', 7, 'oito', 9, 'ten']; var OddArray = MyArray.Filter (Isnumeric); // saídas: 1,3,5,7,9 var OddArray = MyArray.osa (isnumeric); // saídas: true var OddArray = myArray.avery (isnumeric); // saídas: false var printArray = function (x, idx) {document.writeln ('['+idx+'] ='+x); } MyArray.ForEach (PrintArray); // Saídas: [0] = 1 [1] = dois [2] = 3 [3] = quatro [4] = 5 MyArray.Remove (9); document.writeln (MyArray); if (! Array.prototype.avery) {array.prototype.every = function (fun /*, thisp* /) {var len = this.length; if (typeof diversão! = "function") lança new typeError (); var thisp = argumentos [1]; for (var i = 0; i <len; i ++) {if (i neste &&! fun.call (thisp, este [i], i, this)) retorna false; } retornar true; }; } if (! Array.prototype.Filter) {Array.prototype.Filter = function (fun /*, thisp* /) {var len = this.length; if (typeof diversão! = "function") lança new typeError (); var res = new Array (); var thisp = argumentos [1]; for (var i = 0; i <len; i ++) {if (i nisso) {var val = this [i]; // Caso a diversão se afunda disso if (fun.call (thisp, val, i, isto)) res.push (val); }} retornar res; }; } if (! Array.prototype.foreach) {Array.prototype.foreach = function (fun /*, thisp* /) {var len = this.length; if (typeof diversão! = "function") lança new typeError (); var thisp = argumentos [1]; for (var i = 0; i <len; i ++) {if (i neste) diversão.call (thisp, este [i], i, este); }}; } if (! Array.prototype.map) {array.prototype.map = function (fun /*, thisp* /) {var len = this.length; if (typeof diversão! = "function") lança new typeError (); var res = nova matriz (len); var thisp = argumentos [1]; for (var i = 0; i <len; i ++) {if (i nisso) res [i] = fun.call (thisp, este [i], i, este); } retornar res; }; } if (! Array.prototype.some) {Array.prototype.some = function (fun /*, thisp* /) {var len = this.length; if (typeof diversão! = "function") lança new typeError (); var thisp = argumentos [1]; for (var i = 0; i <len; i ++) {if (i neste && fun.call (thisp, this [i], i, este)) retorna true; } retornar false; }; } Array.prototype.sortnum = function () {return this.sort (function (a, b) {return ab;}); } <!- var tmp = [5,9,12,18,56,1,10,42, 'azul', 30, 7,97,53,33,30,35,27,30, '35 ',' bola ',' bolha ']; var trinta = tmp.find (30); // retorna 9, 14, 17 var trinta e tmp.find ('35 '); // Retorna 18 var trintafive = tmp.find (35); // Retorna 15 var hAveBlue = tmp.find ('azul'); // retorna 8 var notfound = tmp.find ('não está lá!'); // retorna false var regexp1 = tmp.find (/^b/); // retorna 8.20 (a primeira letra começa com b) var regexp1 = tmp.find (/^b/i); // Retorna 8,19,20 (o mesmo que acima, mas ignore o caso) -> Array.prototype.find = function (Searchstr) {var returrray = false; para (i = 0; i <this.length; i ++) {if (typeof (searchstr) == 'function') {if (searchstr.test (this [i])) {if (! returrray) {returrray = []} returray.push (i); }} else {if (this [i] === Searchstr) {if (! returrray) {returnArray = []} returnArray.push (i); }}} retornar 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); devolver isso; } <!-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)); // saídas: 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])) retornar false; } if (este [i]! == testarr [i]) retorna false; } retornar true; }去掉数组中重复的值 var a = new Array ("5", "7", "7"); A.Unique ();
Array.prototype.unique = function () {var data = this || []; var a = {}; // 声明一个对象 , javascript 的对象可以当哈希表用 para (var i = 0; i <data.length; i ++) {a [data [i]] = true; // 设置标记 , 把数组的值当下标 , 这样就可以去掉重复的值} data.length = 0; for (var i em a) {// 遍历对象 , 把已标记的还原成数组 this [data.length] = i; } retornar dados; } Array.prototype.addall = função ($ array) {if ($ array == null || $ array.length == 0) return; para (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) retorna true; } retornar false; } Array.prototype.indexOf = function ($ value) {for (var $ i = 0; $ i <this.length; $ i ++) {if (this [$ i] == $ value) retorna $ i; } retornar -1; } if (! Array.prototype.LastIndexof) {Array.prototype.LastIndexOf = function (elt /*, de* /) {var len = this.Length; var de = número (argumentos [1]); if (isnan (de)) {de = len - 1; } else {de = (de <0)? Math.CEIL (de): Math.floor (de); if (de <0) de += len; else if (de> = len) de = len - 1; } para (; de> -1; de-) {if (de este && this [de] === ELT) retornar de; } retornar -1; }; } Array.prototype.insertat = function ($ valor, $ index) {if ($ index <0) this.unshift ($ value); caso contrário, if ($ index> = this.length) this.push ($ valor); caso contrário, this.splice ($ index, 0, $ valor); }根据数组的下标来删除元素
Array.prototype.RemoveByIndex = função ($ n) {if ($ n <0) {// 如果 n <0 , 则不进行任何操作。 retorna isso; } else {return this.slice (0, $ n) .concat (this.slice ($ n+1, this.length)); }}依赖 IndexOf
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; retornar; }}} Array.prototype.swap = function ($ a, $ b) {if ($ a == $ b) return; var $ tmp = this [$ a]; este [$ a] = this [$ b]; este [$ b] = $ tmp; } Array.prototype.max = function () {return Math.max.apply ({}, this); } Array.prototype.min = function () {return Math.min.Apply ({}, este); } 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, irres = 0, i = 0; for (i = 0; i <len; i ++) {if (i <start || irres> = dellen) arr [iarr ++] = this [i]; else {res [irres ++] = this [i]; if (item && irres == Dellen) {arr [iarr ++] = item; }}} if (item && irres <dellen) arr [iarr] = item; for (var i = 0; i <arr.length; i ++) {this [i] = arr [i]; } this.length = arr.length; retornar res; } Array.prototype.shift = function () {if (! This) return []; return this.splice (0,1) [0];}分开添加, cópia rasa, 如果遇到数组 , 复制数组中的元素
Array.prototype.concat = function () {var i = 0; while (i <argumentos.length) {if (typeof argumentos [i] === 'object' && typeof argumentos [i] .splice === 'function' &&! Argumentos [i] .Propertyisenumerable ('length')) {// Not raso abaixo // Array.PrototyPype.Concyply; var j = 0; enquanto (j <argumentos [i] .Length) this.splice (this.length, 0, argumentos [i] [j ++]); i ++; } else {this [this.length] = argumentos [i ++]; }} Retorne isso; } Array.prototype.join = function (separador) {var i = 0, str = ""; while (i <this.length) str+= this [i ++]+separador; retornar str; } Array.prototype.pop = function () {return this.splice (this.length-1,1) [0];} Array.prototype.push = function () {Array.prototype.splice.Apply (this, [this.length, 0] .Concat (Array.ProTypey.Slicely.Slicely.Slicely (this, [this.length, 0] .Concat (Array.ProtyPy.Slicely.Slicely.Slicely (this.Length, 0] .CONCAT (Array.ProtyPy.Slicely.Slicely.Slicely (this. // 这里没有直接处理参数 , 而是复制了一下 retorna este.length; } Array.prototype.Reverse = function () {for (var i = 0; i <this.length/2; i ++) {var temp = this [i]; este [i] = this [this.length-1-i]; Este [this.length-1-i] = temp; } retornar isso; } Array.prototype.slice = function (start, end) {var len = this.length; Iniciar = Iniciar <0? Iniciar+= Len: Iniciar? Iniciar: 0; end = end <0? end+= len: end> len? len: end? end? var i = start; var res = []; while (i <end) {res.push (this [i ++]); } retornar res; } //arr.UnShift(ele1,Ele2,Ele3 ....) Array.prototype.UnShift = function () {Array.prototype.splice.apply (this, [0,0] .CONCAT (Array.protype.slice.Apply (this, argumentos))); }