javascript之prototype总结常用方法

//去左右空格
String.prototype.trim = function()
{
    
return this.replace(/^\s*|\s*$/g,'');
}


//去空格添加至数组集合
String.prototype.splitrim = function(t)

    
return this.trim().split(new RegExp('\\s*'+t+'\\s*')) 
}
 

test 
= " testing   , splitrim ";
var arr 
= test.splitrim(',');
alert(
'"' + arr[0+ '"');
alert(
'"' + arr[1+ '"');

//是否为空
String.prototype.isEmpty = function()
{
    
return this.replace(/^\s*|\s*$/g,'').length == 0;
}


//THML特殊字符--编码
String.prototype.EncodeHTML = function()

    var i,e
={'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',' ':'&nbsp;'},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
={'&lt;':'<','&gt;':'>','&amp;':'&','&quot;':'"','&nbsp;':' '},t=this
    
for(i in e) 
        t
=t.replace(new RegExp(i,'g'),e[i]); 
    
return t; 
}
 

test 
= 'testing &lt;b&gt;unesc&nbsp;Html&lt;/b&gt;';
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=='')
    
{
        
return 0;
    }

   
return this.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;
    
    
return this;
}


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>0return y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s + '.' + ms; 
     
if(h>0 || i>0 || s>0return y + '-' + m + '-' + d + ' ' + h + ':' + i + ':' + s; 
     
return y + '-' + m + '-' + d; 
   }
 
   
return this
}
 

var date 
= new Date();
document.write(date.FormatDate());
原文地址:https://www.cnblogs.com/di305449473/p/1224088.html