zoukankan      html  css  js  c++  java
  • Javascript 对象扩展积累

     
    String.prototype.TrimEnd = function (c) {
            var str = this;
            var rg = new RegExp(c);
            var i = str.length;
            while (rg.test(str.charAt(--i)));
            return str.slice(0, i + 1);
        };

    //过滤特殊字符  

    String.prototype.filterSpecialChar = function () {var pattern = new RegExp("[`~!@@#$^&*()=\\-+|{}':;',\\[\\]\\.%<>/?~!#¥……&*()—|{}【】‘;:”“'。,、?]");var rs = "";for (var i = 0; i < this.length; i++) {rs = rs + this.substr(i, 1).replace(pattern, '');} return rs;}; 

    //eg."asdaisdias^&%&^$&$^#".filterSpecialChar() 

    String.prototype.ToCharArray = function(){return this.split("");}
    String.prototype.Reverse 
    = function(){return this.split("").reverse().join("");}
    String.prototype.IsContains 
    = function(str) { return (this.indexOf(str) > -1); } /* include or not ?*/  

    //格式化"aa{0},bb{1}".Format("1","2")
    String.prototype.Format 
    =function() { var args = arguments; returnthis.replace(/\{(\d+)\}/g, function(m, i, o, n) { return args[i]; }); };

    //eg.alert('<a href="{clickurl}" target="_blank">{inner}</a>'.JsonFormat({clickurl:'http://www.baidu.com',inner:'baidu'}));
    String.prototype.JsonFormat 
    =function (config,reserve) {return this.replace(/\{([^}]*)\}/g,(typeof config=='object')?function (m,i) {var ret=config[i];return ret==null&&reserve?m:ret}:config); }; /*2010-1-3*/
    String.prototype.ResetBlank 
    = function(){return this.replace(/s+/g,"");}
    String.prototype.LTrim 
    = function(){return this.replace(/^s+/g, "");}
    String.prototype.RTrim 
    = function(){return this.replace(/s+$/g, "");}
    String.prototype.Trim 
    = function(){return this.replace(/(^\s*)|(\s*$)/g, ""); }
    String.prototype.GetNum 
    = function(){return this.replace(/[^d]/g, "");} /*num only*/
    String.prototype.GetEn 
    = function(){return this.replace(/[^A-Za-z]/g, ""); } /* english charctar only */
    String.prototype.GetCn 
    = function(){return this.replace(/[^\u4e00-\u9fa5\uf900-\ufa2d]/g, ""); } /* chinese charctar only */
    String.prototype.ByteLength 
    = function(){return this.replace(/[^\x00-\xff]/g, "aa").length; } /* get Byte Length */
    String.prototype.Left 
    = function(n){return this.slice(0, n);}
    String.prototype.Right 
    = function(n){return this.slice(this.length - n);}
    String.prototype.Insert 
    = function(index, str) { return this.substring(0, index) + str + this.substr(index);}
    String.prototype.Copy 
    = function(){if(IE) window.clipboardData.setData("text"this.toString()); }/* ie only */
    String.prototype.HtmlEncode 
    = function(){var i,e ='&''&amp;''<''&lt;''>''&gt;''"''&quot;' },t = thisfor (i in e) t = t.replace(new RegExp(i, 'g'), e[i]);return t}
    String.prototype.UrlEncode 
    = function(){return encodeURIComponent(this); }
    String.prototype.Unicode 
    = function(){var tmpArr = []; for (var i = 0; i < this.length; i++) tmpArr.push("&#" + this.charCodeAt(i) + ";"); return tmpArr.join("");}
    /*Validate*/
    String.prototype.IsEmpty 
    = function() { return this == ""; }
    String.prototype.IsEmail 
    = function(){return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(this);}
    String.prototype.IsChinese 
    = function(){return /^[\u0391-\uFFE5]+$/.test(this);}
    String.prototype.IsQQ 
    = function(){return /^[0-9]{5,9}$/.test(this);}
    String.prototype.IsTel 
    = function(){return /^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}$/.test(this);}
    String.prototype.IsTelAll 
    = function(){return /^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}$/.test(this|| /^\d{11}$/.test(this);} /* include cell phone */
    String.prototype.IsNum 
    = function() { return /^(\d+)$/.test(this); }
    /*-- Array Class Extendtions --*/
    Array.prototype.Add 
    = function(itAdd) { var _index = this.indexOf(itAdd); if (_index < 0) {this.push(itAdd);}};
    Array.prototype.Del 
    = function(itDel) { var _index = this.indexOf(itDel); if (_index >= 0) { this.splice(_index, 1); }}; 

    p

    Date.prototype.addSeconds = function(seconds) {
                this.setSeconds(this.getSeconds() + seconds);
                return this;
            };
    Date.prototype.addMinutes = function(minutes) {
                this.setMinutes(this.getMinutes() + minutes);
                return this;
    };
    
    Date.prototype.addHours = function(hours) {
                this.setHours(this.getHours() + hours);
                return this;
    };
    
    Date.prototype.addDays = function(days) {
                this.setDate(this.getDate() + days);
                return this;
    };
    
    Date.prototype.addWeeks = function(weeks) {
                this.addDays(weeks*7);
                return this;
    };
    
    Date.prototype.addMonths = function (months) {
                var dt = this.getDate();
    
                this.setMonth(this.getMonth() + months);
                var currDt = this.getDate();
    
                if (dt !== currDt) {  
                    this.addDays(-currDt);
                }
    
                return this;
    };
    
    Date.prototype.addYears = function(years) {
                var dt = this.getDate();
    
                this.setFullYear(this.getFullYear() + years);
    
                var currDt = this.getDate();
    
                if (dt !== currDt) {  
                    this.addDays(-currDt);
                }
    
                return this;
    };

    --------------------------------------------------------------------------
    Daniel Chow's Blog - 不管你在哪里,都要有一颗创业的心!
    http://www.cnblogs.com/DanielChow/
  • 相关阅读:
    SQL CREATE DATABASE 语句
    SQL SELECT INTO 语句
    SQL UNION 和 UNION ALL 操作符
    复盘实战营一期毕业典礼----HHR计划----以太入门课--第一课
    抑郁研究所融资历程分享--以太一堂--直播课
    投资人分享答疑----HHR计划----以太直播课第三课
    重新理解《务实创业》---HHR计划--以太一堂第三课
    HHR计划---作业复盘-直播第三课
    电影推荐算法---HHR计划
    一堂优秀学员吕智钊分享----HHR计划----直播课第二课
  • 原文地址:https://www.cnblogs.com/DanielChow/p/1624133.html
Copyright © 2011-2022 走看看