zoukankan      html  css  js  c++  java
  • 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());
  • 相关阅读:
    Mysql添加远程访问权限
    Android下安装应用不成功解决
    Unity 编译apk启动出异常
    Java 实现函数回调
    C# 实现函数回调
    北京数字认证无领导小组讨论总结
    深圳市共进电子 嵌入式软件工程师笔试题
    北京君正和博彦科技笔试体会及总结
    9月5日 华为2014校园招聘的机试题目_C语言版答案
    ios客户端base64上传图片到java服务器遇到的问题
  • 原文地址:https://www.cnblogs.com/di305449473/p/1224088.html
Copyright © 2011-2022 走看看