//去左右空格 String.prototype.trim = function() { returnthis.replace(/^\s*|\s*$/g,''); } //去空格添加至数组集合 String.prototype.splitrim = function(t) { returnthis.trim().split(new RegExp('\\s*'+t+'\\s*')) } test =" testing , splitrim "; var arr = test.splitrim(','); alert('"'+ arr[0] +'"'); alert('"'+ arr[1] +'"'); //是否为空 String.prototype.isEmpty = function() { returnthis.replace(/^\s*|\s*$/g,'').length ==0; } //THML特殊字符--编码 String.prototype.EncodeHTML = function() { var i,e={'&':'&','<':'<','>':'>','"':'"','':' '},t=this; for(i in e) t=t.replace(new RegExp(i,'g'),e[i]); return t; } test ='testing <b>escHtml</b>'; document.write (test.EncodeHTML()); //THML特殊字符--解码 String.prototype.DecodeHTML = function() { var i,e={'<':'<','>':'>','&':'&','"':'"',' ':''},t=this; for(i in e) t=t.replace(new RegExp(i,'g'),e[i]); return t; } test ='testing <b>unesc Html</b>'; document.write (test.DecodeHTML()); //Url编码 String.prototype.UrlEncode = function() { return encodeURIComponent(this); } test ='http://di305449473.cnblogs.com'; document.write (test.UrlEncode()); //Url解码 String.prototype.UrlDecode = function() { return decodeURIComponent(this); } test ='http%3A%2F%2Fdi305449473.cnblogs.com'; document.write (test.UrlDecode()); //验证时候为有效的Email String.prototype.isEmail = function () { //var rx = new RegExp("\\w+([-+.\']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"); //var matches = rx.exec(this); //return (matches != null && this == matches[0]); //或者 var reg =/^\w+([-+.']\w+)*@\w+([-.]\w+)*.\w+([-.]\w+)*$/g; return reg.test(this); } test ='ldgg@vip.qq.com'; document.write (test.isEmail()); //是否为有效的URL地址 String.prototype.isURL = function () { //var rx = new RegExp("http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w-\\+ ./?%:&=#\\[\\]]*)?"); //var matches = rx.exec(this); //return (matches != null && this == matches[0]); //或者 var reg =/^http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w-\+ .\/?%:&=#\[\]]*)?$/g; return reg.test(this); } test ='http://di305449473.cnblogs.com'; document.write (test.isURL()); //是否包含某字符串 String.prototype.Contains = function(str) { var result =this.indexOf(str) >-1?true : false; return result; } var str ='Can you speak English?'; document.write(str.Contains(' speak'.trim())); //格式化 '¥10.00' 形式为 10.00 String.prototype.parsePrice = function() { if (this==null||this=='') { return0; } returnthis.replace(/¥/g,'').replace(/,/g,''); } test ='¥10.00'; document.write(test.parsePrice()); //格式化1000或1000.00000 为'¥1,000.00' Number.prototype.FormatPrice = function() { var t = Math.round(this*100)/100; t = t.toString(); var v='',g=''; var index = t.indexOf('.'); if(index >0) { s = t.substr(0,index); g = t.substr(index,t.length); } else s = t; d = s; if(s.length >3) { for(i=1;i<=d.length/3; i++) { v =','+s.substr(s.length-3,s.length)+v; s = s.substr(0,s.length-3); } v = s+v; } if(/^\d+$/.test(t)) return'¥'+v+'.00'; if(/^\d+\.\d$/.test(t)) return'¥'+ v + g +'0'; if(/^\d+\.\d\d$/.test(t)); return'¥'+ v + g; returnthis; } test =2000.100000; document.write(test.FormatPrice()); //格式化标准格式为简单格式 Date.prototype.FormatDate = function(){ if(this instanceof Date){ var y =this.getFullYear(); var m =this.getMonth() +1; var d =this.getDate(); var h =this.getHours(); var i =this.getMinutes(); var s =this.getSeconds(); var ms =this.getMilliseconds(); if(ms>0) return y +'-'+ m +'-'+ d +''+ h +':'+ i +':'+ s +'.'+ ms; if(h>0|| i>0|| s>0) return y +'-'+ m +'-'+ d +''+ h +':'+ i +':'+ s; return y +'-'+ m +'-'+ d; } returnthis; } var date =new Date(); document.write(date.FormatDate());