문자열 : :
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, '<'). 교체 (/>/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 (새 regexp ($ old, "gm"), $ snew); }变量替换
var a = "나는 {0}을 좋아하고 당신은 {0}!"; a.format ( "당신", "me"); String.prototype.format = function () {var args = arguments; return this.replace (// {(/d+)/}/g, function (m, i, o, n) {return args [i];}); }在字符串末尾追加字符串
String.prototype.append = function ($ str) {return this.concat ($ str); }删除指定索引位置的字符, 索引无效将不删除任何字符
String.prototype.deteleCharat = function ($ sindex) {if ($ sindex <0 || $ sindex> = this.length) {return this.valueof (); } else if ($ sindex == 0) {reture this.substring (1, this.length); } else if ($ sindex == this.length-1) {reture 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; issubstring (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.equalsEngoreCase = 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); } return 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 (); } return this.SubString (0, $ ofset)+$ str+this.SubString ($ ofset+1); } string.prototype.replacelen = function (start, len, 교체) {if (! len) 님이 이것을 반환합니다. if (start> = this.length) 이것을 반환합니다. 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 /> 天 <입력 /> 小时
String.prototype.replaceChar = 함수 (대상, 교체, 시작) {if (! target) retract 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) : 대상; if (c == target) {returnVal += typeof gervaced == 'function'? Call (this, index) : 교체; 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)); } return str; }计算长度, 每个汉字占两个长度, 英文字符每个占一个长度
String.prototype.uclength = function () {var len = 0; for (var i = 0; i <this.length; i ++) {if (this.charcodeat (i)> 255) len+= 2; 그렇지 않으면 Len ++; } 반환 렌; }在字符串的左边填充一些特定的字符
string.prototype.lpad = function (len, s) {var a = 새 배열 (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 = 새 배열 (this); var n = (Len -this.length); for (var i = 0; i <n; i ++) {a.push (s); } return 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? 사실 : 거짓; }짐
String.prototype.todate = function () {var str = this.replace (/-/g, "/"); 반환 (새 날짜 (str)); }将原来用字符串表示的十进数转成十进制浮点数 : 정밀도 ision
String.prototype.tofloat = function (Precision) {Precision = Precision || 2; parsefloat (this, 10) .tofixed (정밀); }将原来用字符串表示的十进数转成十进制整数
String.prototype.toint = function () {return parseint (this, 10) .toString (); }将两个原来用字符串表示的十进数相加后当作字串返回 : 부가 end
String.prototype.add = function (addend) {var sum = parsefloat (this, 10) + parsefloat (addend, 10); 반환 합계+""; }Nextscale 为进制 如 2,8,16
string.prototype.shiftscale = function (nextscale) {return parsefloat (this) .tostring (nextscale); }各进制互相转换 :
이 对象必须是整数
@Param Prescale pres
@param nextscale 要转换成几进制数
string.prototype.scaleshift = function (prescale, nextscale) {return parseint (this, prescale) .tostring (nextscale); }全角 2. docum
String.prototype.dbc2sbc = function () {
return this.replace (/[/uff01-/uff5e]/g, function (a) {return string.fromcharcode (a.charcodeat (0) -65248);}). 대체 (// u3000/g, "");
}
배열 扩展函数 :
var isnumeric = function (x) {// x가 숫자이고 그렇지 않은 경우 false 인 경우 true를 반환합니다. var regexp = /^(-)? (/d*)(/.?)(/d*)$/; return string (x) .match (regexp); } var myarray = [1, 'two', 3, 'four', 5, 'six', 7, '8', 9, 'ten']; var OddArray = myArray.filter (isnumeric); // 출력 : 1,3,5,7,9 var OddArray = MyArray.some (isnumeric); // outputs : true var 홀수 array = myArray.every (isnumeric); // 출력 : false var printArray = function (x, idx) {document.writeln ( '['+idx+'] ='+x); } myArray.foreach (printArray); // 출력 : [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") 새 typeerror ()를 던지십시오. var thisp = 인수 [1]; for (var i = 0; i <len; i ++) {if (i this &&! fun.call (thisp, thisp, this [i], i, this))) false; } 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 in this) {var val = this [i]; // FUN이 If (Fun.Call (Thisp, Val, I, This)) res.push (val); }} return 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 in this) fun.call (thisp, 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 = 새로운 배열 (Len); var thisp = 인수 [1]; for (var i = 0; i <len; i ++) {if (i if this) res [i] = fun.call (thisp, thisp, this [i], i, this); } return 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 this && fun.call (thisp, thisp, this [i], i, this)))). } false를 반환합니다. }; } array.prototype.sortnum = function () {return this.sort (a, b) {return ab;}); . var Thretty = tmp.find (30); // 반환 9, 14, 17 var ThirtyFive = tmp.find ('35 '); // 반환 18 var ThirtyFive = tmp.Find (35); // 반환 15 var hadblue = tmp.find ( 'blue'); // 반환 8 var notfound = tmp.find ( '거기!'); // false var 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 (typeStrs) == '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 [rnd], this [rnd] = tmp); 이것을 반환하십시오; } <!-var myarray = [1,2,3,4,5,6,7,8,9,10,11,12,14,15]; var yourray = [1,2,3,4,5,6,7,8,9,9,10,11,13,14,15]; document.writeln (myarray.compare (YourArray)); // outputs : 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 = 새 배열 ( "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 $ 요소 = 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; } 반환 -1; } if (! array.prototype.lastIndexof) {array.prototype.lastIndexof = function (elt /*, from* /) {var len = this.length; var from = 숫자 (인수 [1]); if (isnan (from)) {from = len -1; } else {from = (<0)? Math.ceil (From) : Math.floor (From); if (<0에서)에서 += len; 그렇지 않으면 if (from> = len)에서 = len -1; } for (; from> -1; our--) {if (this && this [from this [from] === elt) reture from에서; } 반환 -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) {// 如果 n <0, 则不进行任何操作。 this; } 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; 반품; }}} array.prototype.swap = function ($ a, $ b) {if ($ a == $ b) return; 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.prototype.splice = function (start, dellen, item) {var len = this.length; 시작 = 시작 <0? 0 : start> len? len : 시작? 시작 : 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 ++] = 항목; }}} if (item && ires <dellen) arr [iarr] = 항목; for (var i = 0; i <arr.length; i ++) {this [i] = arr [i]; } this.length = arr.length; 리턴 레스; } array.prototype.shift = function () {if (! this) return []; return this.splice (0,1) [0];}分开添加, ow 얕은 사본, 如果遇到数组, 复制数组中的元素
Array.prototype.concat = function () {var i = 0; while (i <arguments.length) {if (typeof arguments [i] === 'object'&& type -optionof arguments [i] .splice === 'function'&&! arguments [i] .propertyisEnumerable ( 'length')) {// array.prototype.concat.apply (이것 [i ++]); var j = 0; while (j <arguments [i] .length) this.splice (this.length, 0, arguments [i] [j ++]); i ++; } else {this [this.length] = 인수 [i ++]; }} 님이 reture; } array.prototype.join = function (분리기) {var i = 0, str = ""; while (i <this.length) str+= this [i ++]+분리기; Return str; } array.prototype.pop = function () {return this.splice (this.length-1,1) [0];} array.prototype.push = function () {array.prototyp.splice.apply (this, length, 0] .concat (array.prototype.sapply (Arguments)); // 这里没有直接处理参数, 这里没有直接处理参数 this.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] = 온도; } 이것을 반환하십시오. } array.prototype.slice = function (start, end) {var len = this.length; start = start <0? start+= len : 시작? 시작 : 0; 종료 = end <0? end+= len : end> len? len : end? end : len; var i = 시작; var res = []; while (i <end) {res.push (this [i ++]); } return res; } //arr.unshift(ELE1,ELE2,ELE3 ....) ARRAY.PROTOTYP.UNSHIFT = function () {Array.Prototype.splice.Apply (this, [0,0] .concat (Array.Prototype.slice.Apply (this, Arguments)); }