PS:只是在工作中常用,自己分享下,希望大神勿喷。
判断空对象
function isEmpty(e) {
var t;
for (t in e)
return !1;
return !0
}
//空的话 ---返回true
//非空的话---返回false
数组扩展: //删除指定项
Array.prototype.removeValue = function(val) {
for(var i=0; i<this.length; i++) {
if(this[i] == val) {
this.splice(i, 1);
break;
}
}
};
var a = [1,2,3,4,5];
a.removeValue (2);
字符串在指定位置插入字符
参数说明:str表示原字符串变量,flg表示要插入的字符串,sn表示要插入的位置
var newstr="";
for(var i=0;i<str.length;i+=sn){
var tmp=str.substring(i, i+sn);
newstr+=tmp+flg;
}
return newstr;
}
将字符串中所有的空格(一个或者多个)都替换成一个
String.prototype.ResetBlank=function(){
var regEx = /\s+/g;
return this.replace(regEx, ' ');
};
var str=' 213 345 675 7887';
str.ResetBlank();
字符串中去掉首尾的逗号
replace(/^,+/,"").replace(/,+$/,"")