正则表达式

来源:互联网 发布:岚锋创视网络 编辑:程序博客网 时间:2024/05/16 10:34
 <script language="javascript">

// 合并多个空白为一个空白
   String.prototype.resetBlank = function()
   {
       var regEx = //s+/g;
       return this.replace(regEx,' ');      
   }

// 除去左边空白
   String.prototype.Ltrim = function()
   {
       var regEx = /^/s+/g;
       return this.replace(regEx,'');              
   }
   
// 除去右边空白
   String.prototype.Rtrim = function()
   {
       var regEx = //s+$/g;
       return this.replace(regEx,'');              
   }
    
// 除去两边空白
   String.prototype.Trim = function()
   {
       var regEx = /(^/s+)|(/s+$)/g;
       return this.replace(regEx,'');              
   }
   
// 除去所有空白
   String.prototype.Trim = function()
   {
       var regEx = //s*/g;
       return this.replace(regEx,'');              
   }
   
// 保留数字
   String.prototype.GetNum = function()
   {
       var regEx = /[^/d]/g;
       return this.replace(regEx,'');              
   }

// 保留字母
   String.prototype.GetEn = function()
   {
       var regEx = /[^A-Za-z]/g;
       return this.replace(regEx,'');              
   }

// 保留大写字母
   String.prototype.GetUcase = function()
   {
       var regEx = /[^A-Z]/g;
       return this.replace(regEx,'');              
   }
   
// 保留小写字母
   String.prototype.GetLcase = function()
   {
       var regEx = /[^a-z]/g;
       return this.replace(regEx,'');              
   }

// 保留中文
   String.prototype.GetCn = function()
   {
       var regEx = /[^/u4e00-/u9fa5/uf900-/ufa2d]/g;
       return this.replace(regEx,'');   
   }

// 得到子节长度
   String.prototype.GetRealLength = function()
   {   
       var regEx = /^[/u4e00-/u9fa5/uf900-/ufa2d]+$/;
       if (regEx.test(this))
       {   
           return this.length * 2;
       } else {
           var oMatches = this.match(/[/x00-/xff]/g);
           var oLength = this.length * 2 - oMatches.length;
           return oLength;
       }   
   }

// 左边截取字符串
   String.prototype.Left = function(oLength)
   {
       return this.slice(0,oLength);   
   }

// 右边截取字符串
   String.prototype.Right = function(oLength)
   {
       if (this.length<oLength)
       {
           return this;
       } else {
           return this.slice(this.length-oLength);
       }   
   }

// 获取文件全名
   String.prototype.GetFileName = function()  
   {
      var regEx = /^.*//([^///?]*).*$/;
      return this.replace(regEx,'$1');
   }

// 获取文件扩展名
   String.prototype.GetExtensionName = function()  
   {
      var regEx = /^.*//[^//]*(/.[^/./?]*).*$/;
      return this.replace(regEx,'$1');
   }
 
// 获取文本内容
   String.prototype.GetInnerText = function()  
   {
      var regEx = /<//?[^>]*>/g;
      return this.replace(regEx,'');
   }

// 监测是否为空    
   String.prototype.isEmail = function()
   {
       return (this=='');   
   }

// 监测是否相等    
   String.prototype.isEquals = function(oString)
   {
       return (this==oString);   
   }

// 监测邮箱格式      
   String.prototype.isEmail = function()
   {
       var regEx = /^/w+((-/w+)|(/./w+))*/@[A-Za-z0-9]+((/.|-)[A-Za-z0-9]+)*/.[A-Za-z0-9]+$/;
       return regEx.test(this);   
   }

// String转化为Number
   String.prototype.Int = function()
   {
       return isNaN(parseInt(this)) ? this.toString() : parseInt(this);
   }

// HTML编码
   String.prototype.HTMLEncode = function()
   {
       var objThis = this;
       var oRegExp = new Array(//x26/g,//x3c/g,//x3e/g,//x3e/g,//x27/g);
       var oTarget = new Array('&','<','>','"','/'');
       for (var i=0; i<5; i++)
       {
           objThis = objThis.replace(oRegExp[i], oTarget[i]);
       }
       return objThis;
   }

// 字符串格式化
   String.prototype.encodeStr = function()
   {
       var objThis = this;
       objThis = objThis.HTMLEncode();
       objThis = objThis.replace(//n/g, '<br>');
       objThis = objThis.replace(//t/g, '      ');
       objThis = objThis.replace(//s/g, ' ');
       return objThis;       
   }

// Unicode转化
   String.prototype.AscW = function()
   {
       var strText = '';
       for (var i=0; i<this.length; i++)
       {
           strText += '&#' + this.charCodeAt(i);
       }
       return strText;
   }

// 数字补零
   Number.prototype.LengthWithZero = function(oCount)
   {
       var strText = this.toString();
       while (strText.length<oCount)
       {
           strText = '0' + strText;   
       }
       return strText;
   }
   
// Unicode还原
   Number.prototype.ChrW = function()
   {
       return String.fromCharCode(this);     
   }

// 数字数组由小到大排序
   Array.prototype.Min2Max = function()
   {   
       var oValue;
       for (var i=0; i<this.length; i++)
       {
           for (var j=0; j<=i; j++)
           {
               if (this[i]<this[j])
               {
                  oValue = this[i];
                  this[i] = this[j];
                  this[j] = oValue;
               }
           }  
       }
       return this;
   }

// 数字数组由大到小排序
   Array.prototype.Max2Min = function()
   {
       var oValue;
       for (var i=0; i<this.length; i++)
       {
           for (var j=0; j<=i; j++)
           {
               if (this[i]>this[j])
               {
                  oValue = this[i];
                  this[i] = this[j];
                  this[j] = oValue;
               }
           }  
       }
       return this;
   }

// 获得数组值
   Array.prototype.Item = function(oIndex)
   {
       return this[oIndex];
   }

// 删除数组指定项
   Array.prototype.removeItem = function(oIndex)
   {
       this.splice(oIndex,1);
   }

// 正向查找
   Array.prototype.InStr = function(oTarget)
   {
       for (var i=0; i<this.length; i++)
       {
           if (this[i]=oTarget && typeof(this[i])==typeof(oTarget))
           {
               return i;
           }
       }
       return -1;  
   }

// 反向查找
   Array.prototype.LastInStr = function(oTarget)
   {
       for (var i=this.length-1; i>=0; i--)
       {
           if (this[i]=oTarget && typeof(this[i])==typeof(oTarget))
           {
               return i;
           }
       }
       return -1;
   }

// 判断是否存在
   Array.prototype.InStrBolean = function(oTarget)
   {
       for (var i=0; i<this.length; i++)
       {
           if (this[i].oTarget && typeof(this[i])==typeof(oTarget))
           {
               return true;
           }
       }
       return false;
   }

// 获得数字数组中最大项
   Array.prototype.GetMax = function()
   {
       var oValue = 0;
       for(var i=0; i<this.length; i++)
       {
           if (this[i]>oValue)
           {
               oValue = this[i];
           }  
       }
       return oValue;
   }

// 获得数字数组中最小项
   Array.prototype.GetMin = function()
   {
       var oValue = 0;
       for(var i=0; i<this.length; i++)
       {
           if (this[i]<oValue)
           {
               oValue = this[i];
           }  
       }
       return oValue;
   }

// 获取当前时间的GB形式
   Date.prototype.GetCNDate = function()
   {
      var oDateText = '';
      oDateText += this.getFullYear().LengthWithZero(4) + new Number(24180).ChrW();
      oDateText += this.getMonth().LengthWithZero(2) + new Number(26376).ChrW();
      oDateText += this.getDate().LengthWithZero(2) + new Number(26085).ChrW();
      oDateText += this.getHours().LengthWithZero(2) + new Number(26102).ChrW();
      oDateText += this.getMinutes().LengthWithZero(2) + new Number(20998).ChrW();
      oDateText += this.getSeconds().LengthWithZero(2) + new Number(31186).ChrW();
      oDateText += new Number(32).ChrW() + new Number(32).ChrW() + new Number(26143).ChrW() + new Number(26399).ChrW() + new String('26085199682010819977222352011620845').substr(this.getDay()*5,5).Int().ChrW();
      return oDateText;    
   }

// 得到对象的绝对坐标
   function GetPosition(obj)
   {
      var objThis = obj;
      var oBody = document.body;
      var oLeft = oTop = 0;
      while (objThis!=oBody)
      {
          oLeft += objThis.offsetLeft;
          oTop += objThis.offsetTop;
          objThis = objThis.offsetParent;
      }
      return { left: oLeft, top: oTop };  
   }

// 取一定范围的随机数
   function GetRndNum(lLimit, uLimit)
   {
       var oRndNum = Math.floor((uLimit-lLimit+1)*Math.random()+lLimit);
       return oRndNum;
   }

// 获取指定位数的随机密码
   function GetRndPwd(oLength)
   {
       var oRndPwd = '';
       var oTempNum;
       var oArray = new Array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
       while (oRndPwd.length<oLength)
       {
           oTempNum = GetRndNum(0,61);
           oRndPwd += oArray[oTempNum].toString();
       }
       return oRndPwd;
   }

// 定义圆类
   function Circle(xPoint, yPoint, oRadius)
   {
      this.X = xPoint;
      this.Y = yPoint;
      this.R = oRadius;
      this.PI = Math.PI;
      this.circleArea = function()
      {
          return this.PI * this.R * this.R;
      }
      this.circleLength = function()
      {
          return 2 * this.PI * this.R;
      }
   }
 
</script>
原创粉丝点击