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('{inner}'.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  = ' & ' ' & ' ' < ' ' < ' ' > ' ' > ' ' " ' ' " '  },t  =   this for  (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;
};

 

转载于:https://www.cnblogs.com/DanielChow/archive/2009/12/14/1624133.html

你可能感兴趣的:(Javascript 对象扩展积累)