javascript中replace方法的高级替换

===========================================
//去除左边的空格
===========================================

JavaScript代码
        
  1. */
  2.     
  3. String.prototype.LTrim =function()
  4.     
  5. {
  6.     
  7. returnthis.replace(/(^s*)/g,"");
  8.     
  9. }
  10.     
  11. /*

===========================================
//去除右边的空格
===========================================

JavaScript代码
        
  1. */
  2.     
  3. String.prototype.Rtrim =function()
  4.     
  5. {
  6.     
  7. returnthis.replace(/(s*$)/g,"");
  8.     
  9. }
  10.     
  11. /*


===========================================
//去除前后空格
===========================================

JavaScript代码
        
  1. */
  2.     
  3. String.prototype.Trim =function()
  4.     
  5. {
  6.     
  7. returnthis.replace(/(^s*)|(s*$)/g,"");
  8.     
  9. }
  10.     
  11. /*


===========================================
//得到左边的字符串
===========================================

JavaScript代码
        
  1. */
  2.     
  3. String.prototype.Left =function(len)
  4.     
  5. {
  6.     
  7.  
  8.     
  9. if(isNaN(len)||len==null)
  10.     
  11. {
  12.     
  13. len =this.length;
  14.     
  15. }
  16.     
  17. else
  18.     
  19. {
  20.     
  21. if(parseInt(len)<0||parseInt(len)>this.length)
  22.     
  23. {
  24.     
  25. len =this.length;
  26.     
  27. }
  28.     
  29. }
  30.     
  31.  
  32.     
  33. returnthis.substr(0,len);
  34.     
  35. }
  36.     
  37. /*


===========================================
//得到右边的字符串
===========================================

 

JavaScript代码
        
  1. */
  2.     
  3. String.prototype.Right =function(len)
  4.     
  5. {
  6.     
  7.  
  8.     
  9. if(isNaN(len)||len==null)
  10.     
  11. {
  12.     
  13. len =this.length;
  14.     
  15. }
  16.     
  17. else
  18.     
  19. {
  20.     
  21. if(parseInt(len)<0||parseInt(len)>this.length)
  22.     
  23. {
  24.     
  25. len =this.length;
  26.     
  27. }
  28.     
  29. }
  30.     
  31.  
  32.     
  33. returnthis.substring(this.length-len,this.length);
  34.     
  35. }
  36.     
  37. /*


===========================================
//得到中间的字符串,注意从0开始
===========================================

 

JavaScript代码
        
  1. */
  2.     
  3. String.prototype.Mid =function(start,len)
  4.     
  5. {
  6.     
  7. returnthis.substr(start,len);
  8.     
  9. }
  10.     
  11. /*


===========================================
//在字符串里查找另一字符串:位置从0开始
===========================================

 

JavaScript代码
        
  1. */
  2.     
  3. String.prototype.InStr =function(str)
  4.     
  5. {
  6.     
  7.  
  8.     
  9. if(str==null)
  10.     
  11. {
  12.     
  13. str ="";
  14.     
  15. }
  16.     
  17.  
  18.     
  19. returnthis.indexOf(str);
  20.     
  21. }
  22.     
  23. /*


===========================================
//在字符串里反向查找另一字符串:位置0开始
===========================================

 

JavaScript代码
        
  1. */
  2.     
  3. String.prototype.InStrRev =function(str)
  4.     
  5. {
  6.     
  7.  
  8.     
  9. if(str==null)
  10.     
  11. {
  12.     
  13. str ="";
  14.     
  15. }
  16.     
  17.  
  18.     
  19. returnthis.lastIndexOf(str);
  20.     
  21. }
  22.     
  23. /*

===========================================
//计算字符串打印长度
===========================================

 

JavaScript代码
        
  1. */
  2.     
  3. String.prototype.LengthW =function()
  4.     
  5. {
  6.     
  7. returnthis.replace(/[^x00-xff]/g,"**").length;
  8.     
  9. }
  10.     
  11. /*


===========================================
//是否是正确的IP地址
===========================================

 

======

JavaScript代码
        
  1. */
  2.     
  3. String.prototype.isIP =function()
  4.     
  5. {
  6.     
  7.  
  8.     
  9. varreSpaceCheck = /^(d+).(d+).(d+).(d+)$/;
  10.     
  11.  
  12.     
  13. if(reSpaceCheck.test(this))
  14.     
  15. {
  16.     
  17. this.match(reSpaceCheck);
  18.     
  19. if(RegExp.$1 <= 255 && RegExp.$1 >= 0
  20.     
  21. && RegExp.$2 <= 255 && RegExp.$2 >= 0
  22.     
  23. && RegExp.$3 <= 255 && RegExp.$3 >= 0
  24.     
  25. && RegExp.$4 <= 255 && RegExp.$4 >= 0)
  26.     
  27. {
  28.     
  29. returntrue;
  30.     
  31. }
  32.     
  33. else
  34.     
  35. {
  36.     
  37. returnfalse;
  38.     
  39. }
  40.     
  41. }
  42.     
  43. else
  44.     
  45. {
  46.     
  47. returnfalse;
  48.     
  49. }
  50.     
  51.  
  52.     
  53. }
  54.     
  55. /*

=====================================
//是否是正确的长日期
=====================================

 

JavaScript代码
        
  1. */
  2.     
  3. String.prototype.isLongDate =function()
  4.     
  5. {
  6.     
  7. varr =this.replace(/(^s*)|(s*$)/g,"").match(/^(d{1,4})(-|/)(d{1,2})2(d{1,2}) (d{1,2}):(d{1,2}):(d{1,2})$/);
  8.     
  9. if(r==null)
  10.     
  11. {
  12.     
  13. returnfalse;
  14.     
  15. }
  16.     
  17. vard =newDate(r[1], r[3]-1,r[4],r[5],r[6],r[7]);
  18.     
  19. return(d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]&&d.getHours()==r[5]&&d.getMinutes()==r[6]&&d.getSeconds()==r[7]);
  20.     
  21.  
  22.     
  23. }
  24.     
  25. /*

===========================================
//是否是正确的短日期
===========================================

 

JavaScript代码
        
  1. */
  2.     
  3. String.prototype.isShortDate =function()
  4.     
  5. {
  6.     
  7. varr =this.replace(/(^s*)|(s*$)/g,"").match(/^(d{1,4})(-|/)(d{1,2})2(d{1,2})$/);
  8.     
  9. if(r==null)
  10.     
  11. {
  12.     
  13. returnfalse;
  14.     
  15. }
  16.     
  17. vard =newDate(r[1], r[3]-1, r[4]);
  18.     
  19. return(d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]);
  20.     
  21. }
  22.     
  23.  
  24.     
  25. /*

===========================================
//是否是正确的日期
===========================================

 

JavaScript代码
        
  1. */
  2.     
  3. String.prototype.isDate =function()
  4.     
  5. {
  6.     
  7. returnthis.isLongDate()||this.isShortDate();
  8.     
  9. }
  10.     
  11.  
  12.     
  13. /*


===========================================
//是否是手机
===========================================

JavaScript代码
        
  1. */
  2.     
  3. String.prototype.isMobile =function()
  4.     
  5. {
  6.     
  7. return/^0{0,1}13[0-9]{9}$/.test(this);
  8.     
  9. }
  10.     
  11.  
  12.     
  13. /*


===========================================
//是否是邮件
===========================================

 

JavaScript代码
        
  1. */
  2.     
  3. String.prototype.isEmail =function()
  4.     
  5. {
  6.     
  7. return/^w+((-w+)|(.w+))*@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+)*.[A-Za-z0-9]+$/.test(this);
  8.     
  9. }
  10.     
  11.  
  12.     
  13. /*


===========================================
//是否是邮编(中国)
===========================================

JavaScript代码
        
  1. */
  2.     
  3.  
  4.     
  5. String.prototype.isZipCode =function()
  6.     
  7. {
  8.     
  9. return/^[d]{6}$/.test(this);
  10.     
  11. }
  12.     
  13.  
  14.     
  15. /*


===========================================
//是否是有汉字
===========================================

 

JavaScript代码
        
  1. */
  2.     
  3. String.prototype.existChinese =function()
  4.     
  5. {
  6.     
  7. //[u4E00-u9FA5]??字╋[uFE30-uFFA0]?全角符?
  8.     
  9. return/^[x00-xff]*$/.test(this);
  10.     
  11. }
  12.     
  13.  
  14.     
  15. /*

===========================================
//是否是合法的文件名/目录名
===========================================

 

JavaScript代码
        
  1. */
  2.     
  3. String.prototype.isFileName =function()
  4.     
  5. {
  6.     
  7. return!/[/*?|:"<>]/g.test(this);
  8.     
  9. }
  10.     
  11.  
  12.     
  13. /*


===========================================
//是否是有效链接
===========================================

 

JavaScript代码
        
  1. */
  2.     
  3. String.prototype.isUrl =function()
  4.     
  5. {
  6.     
  7. return/^http[s]?://([w-]+.)+[w-]+([w-./?%&=]*)?$/i.test(this);
  8.     
  9. }
  10.     
  11. /*

===========================================
//是否是有效的身份证(中国)
===========================================

 

JavaScript代码
        
  1. */
  2.     
  3. String.prototype.isIDCard =function()
  4.     
  5. {
  6.     
  7. variSum=0;
  8.     
  9. varinfo="";
  10.     
  11. varsId =this;
  12.     
  13.  
  14.     
  15. varaCity={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"};
  16.     
  17.  
  18.     
  19. if(!/^d{17}(d|x)$/i.test(sId))
  20.     
  21. {
  22.     
  23. returnfalse;
  24.     
  25. }
  26.     
  27. sId=sId.replace(/x$/i,"a");
  28.     
  29. //非法地区
  30.     
  31. if(aCity[parseInt(sId.substr(0,2))]==null)
  32.     
  33. {
  34.     
  35. returnfalse;
  36.     
  37. }
  38.     
  39.  
  40.     
  41. varsBirthday=sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2));
  42.     
  43.  
  44.     
  45. vard=newDate(sBirthday.replace(/-/g,"/"))
  46.     
  47.  
  48.     
  49. //非法生日
  50.     
  51. if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) +"-"+ d.getDate()))
  52.     
  53. {
  54.     
  55. returnfalse;
  56.     
  57. }
  58.     
  59. for(vari = 17;i>=0;i--)
  60.     
  61. {
  62.     
  63. iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11);
  64.     
  65. }
  66.     
  67.  
  68.     
  69. if(iSum%11!=1)
  70.     
  71. {
  72.     
  73. returnfalse;
  74.     
  75. }
  76.     
  77. returntrue;
  78.     
  79.  
  80.     
  81. }
  82.     
  83. /*

===========================================
//是否是有效的电话号码(中国)
===========================================

 

JavaScript代码
        
  1. */
  2.     
  3. String.prototype.isPhoneCall =function()
  4.     
  5. {
  6.     
  7. return/(^[0-9]{3,4}-[0-9]{3,8}$)|(^[0-9]{3,8}$)|(^([0-9]{3,4})[0-9]{3,8}$)|(^0{0,1}13[0-9]{9}$)/.test(this);
  8.     
  9. }
  10.     
  11. /*


===========================================
//是否是数字
===========================================

 

JavaScript代码
        
  1. */
  2.     
  3. String.prototype.isNumeric =function(flag)
  4.     
  5. {
  6.     
  7. //验证是否是数字
  8.     
  9. if(isNaN(this))
  10.     
  11. {
  12.     
  13.  
  14.     
  15. returnfalse;
  16.     
  17. }
  18.     
  19.  
  20.     
  21. switch(flag)
  22.     
  23. {
  24.     
  25.  
  26.     
  27. casenull://数字
  28.     
  29. case"":
  30.     
  31. returntrue;
  32.     
  33. case"+"://正数
  34.     
  35. return/(^+?|^d?)d*.?d+$/.test(this);
  36.     
  37. case"-"://负数
  38.     
  39. return/^-d*.?d+$/.test(this);
  40.     
  41. case"i"://整数
  42.     
  43. return/(^-?|^+?|d)d+$/.test(this);
  44.     
  45. case"+i"://正整数
  46.     
  47. return/(^d+$)|(^+?d+$)/.test(this);
  48.     
  49. case"-i"://负整数
  50.     
  51. return/^[-]d+$/.test(this);
  52.     
  53. case"f"://浮点数
  54.     
  55. return/(^-?|^+?|^d?)d*.d+$/.test(this);
  56.     
  57. case"+f"://正浮点数
  58.     
  59. return/(^+?|^d?)d*.d+$/.test(this);
  60.     
  61. case"-f"://负浮点数
  62.     
  63. return/^[-]d*.d$/.test(this);
  64.     
  65. default://缺省
  66.     
  67. returntrue;
  68.     
  69. }
  70.     
  71. }
  72.     
  73. /*

===========================================
//是否是颜色(#FFFFFF形式)
===========================================

JavaScript代码
        
  1. */
  2.     
  3. String.prototype.IsColor =function()
  4.     
  5. {
  6.     
  7. vartemp =this;
  8.     
  9. if(temp=="")returntrue;
  10.     
  11. if(temp.length!=7)returnfalse;
  12.     
  13. return(temp.search(/#[a-fA-F0-9]{6}/) != -1);
  14.     
  15. }
  16.     
  17.  
  18.     
  19. /*


===========================================
//转换成全角
===========================================

 

JavaScript代码
        
  1. */
  2.     
  3. String.prototype.toCase =function()
  4.     
  5. {
  6.     
  7. vartmp ="";
  8.     
  9. for(vari=0;i<this.length;i++)
  10.     
  11. {
  12.     
  13. if(this.charCodeAt(i)>0&this.charCodeAt(i)<255)
  14.     
  15. {
  16.     
  17. tmp += String.fromCharCode(this.charCodeAt(i)+65248);
  18.     
  19. }
  20.     
  21. else
  22.     
  23. {
  24.     
  25. tmp += String.fromCharCode(this.charCodeAt(i));
  26.     
  27. }
  28.     
  29. }
  30.     
  31. returntmp
  32.     
  33. }
  34.     
  35. /*


===========================================
//对字符串进行Html编码
===========================================

 

JavaScript代码
        
  1. */
  2.     
  3. String.prototype.toHtmlEncode =function()
  4.     
  5. {
  6.     
  7. varstr =this;
  8.     
  9.  
  10.     
  11. str=str.replace(/&/g,"&");
  12.     
  13. str=str.replace(/</g,"<");
  14.     
  15. str=str.replace(/>/g,">");
  16.     
  17. str=str.replace(/'/g,"&apos;");
  18.     
  19. str=str.replace(/"/g,""");
  20.     
  21. str=str.replace(/n/g,"<br>");
  22.     
  23. str=str.replace(/ /g," ");
  24.     
  25. str=str.replace(/t/g," ");
  26.     
  27.  
  28.     
  29. returnstr;
  30.     
  31. }
  32.     
  33. /*


===========================================
//转换成日期
===========================================

 

JavaScript代码
        
  1. */
  2.     
  3. String.prototype.toDate =function()
  4.     
  5. {
  6.     
  7. try
  8.     
  9. {
  10.     
  11. returnnewDate(this.replace(/-/g,"/"));
  12.     
  13. }
  14.     
  15. catch(e)
  16.     
  17. {
  18.     
  19. returnnull;
  20.     
  21. }
  22.     
  23. }
  24.     
  25. /*


1.Asc(x),Chr(x):转换字符,字符码

2。Filter:搜寻字符串数组中的特定字符串

格式:v=filter(x,s[,include[,compare]])

实例:

Dim x()={"kjwang","wangkj","peter"}

Dim v

v=filter(x,"kj") '结果v(0)="kjwang",v(1)="wangkj"

v=filter(x,"kj",false) '结果v(0)="peter"

v=filter(x,"kj",true,vbTextCompare) '不分大小写搜寻

3.InStr:寻找字符串位置(InstrRev:倒过来寻找字符串)

格式:

v=instr(x,y) '从x字符串第1个字符起找出y字符串出现的位置

v=instr(n,x,y) '从x字符串第n个字符起找出y字符串出现的位置

格式:

v=InstrRev(x,s[,n[,Compare]])

4。Join:将字符串连接

格式:v=join(x[,d])’d为分隔字符

5。Len(x):计算字符串x的长度

格式:v=len(x)

6.Left(x,n):返回字符串x左边n个字符(对应Right(x,n))

7。Mid:读取字符串x中间的字符

格式:v=mid(x,n,m)

8。LTrim(x),RTim(x),Trim(x)去空白字符

9.Replace:字符串取代

格式:v=Replace(x,s,r)

实例:x="i saw a saw a saw"

v=replace(x,"saw","so") 'v="i so a so a so"

10.Split:字符串分割

格式:v=split(s[,d])

实例:v=split("vb.net,iis6.0,asp.net",",")

'结果v(0)="vb.net",v(1)="iis6.0",v(2)="asp.net"

11.StrReverse:反转字符串

实例:v=strreverse("kjwang") 'v="gnawjk"

12.UCase(x),LCase(x):变换英文字母的大小写

实例:x="hello,VB中文!"

v=UCase(x) 'v="HELLO,VB中文!"

13.取出日期时间

-1)DateValue(x),TimeValue(x)

格式:v=DateValue(x) :取出的“日期”部分

v=timevalue(x) '类上

-2)Year(x),Month(x),Day(x)

格式:v=Year(x)

v=Month(x)

v=Day(x)

Hour(x),Minute(x),Second(x):取出时,分,秒

-3)DateSerial(Y,M,D):合并年、月、日成为日期

实例:Dim v

v=DateSerial(1996,10,5) 'v=1996/10/5

TimeSerial(H,M,S):合并时、分、秒成为时间

14.Now:读取系统日期时间

格式:v=now

15.Timer:从凌晨12:00:00AM起所经过的秒数

格式:v=timer

16.DatePart(p,x):取出年、月、日等各部分的数值

实例:Dim X=#2/10/1996 16:45:30#

v=DatePart("yyyy",X) 'v=1996年

v=DatePart("m",X) 'v=2月

v=DatePart("d",X) 'v=10日

v=DatePart("h",X) 'v=16时

v=DatePart("n",X) 'v=45分

v=DatePart("s",X) 'v=30秒

v=DatePart("q",X) 'v=1季(第一季,春季)

v=DatePart("y",X) 'v=41天(1996年的第41天)

v=DatePart("ww",X) 'v=6周(1996年的第6周)

v=DatePart("w",X) 'v=7(第6周的第7天,即星期六)

17。DateAdd(p,n,x):加减若干时间后的新日期时间

格式:v=DateAdd(p,n,x) 'p值同上如:"yyyy"、"m"等

实例:Dim x=#1/31/1996 16:45:30#

v=dateadd("yyyy",-3,x) '减3年,v=1993/1/31 16:45:30

18.DateDiff(p,x1,x2):计算两日期时间的差值 'p值同上

实例:dim x1=#12/31/1996 16:45:30#

x2=#2/10/1997 9:30:50#

v=datediff("yyyy",x1,x2) 'v=1年

19。FormatDateTime:日期时间的格式化

格式:v=formatdateyime(x[,日期格式])

日期格式值:DateFormat.GeneralDate 、 DateFormat.LongDate、

DateFotmat.ShortDate、DateFormat.LongTime、DateFormat.ShortTime

20.MonthName:返回月份名称

实例:v=monthname(5) 'v="五月"

21.WeekDayName:返回星期名称 ’用法同8

如果您觉得我的文章有帮助,请随意赞赏!

此处评论已关闭