正则表达式

一.正则表达式基础

0.g表示全局,i表示不区分大小写,m表示多行模式。

1.点字符(英文句号)可以匹配任何一个单个字符。

2.反斜杠表示转义。例.表示匹配点字符而不是任何一个单个字符。

3.[ns]表示匹配n或s。

4.[0-9]表示[0123456789],[a-z][A-Z]同理。

5.元字符^来表明对一个字符集合进行取非匹配。例[^0-9]表示出0-9以外的字符。

6.f(换页符), (换行符), (回车符), (制表符),v(垂直制表符)。

换行符:一个长行截成俩行还属于同一段落。

回车符:分成俩个段落了。

7.d:任何一个数字字符。相当于[0-9]。

 D:任何一个非数字字符。相当于[^0-9]。

 w:任何一个字母数字字符(大小写均可)或下划线。相当于[a-zA-Z0-9]。

 W:[^a-zA-Z0-9]。

 s:任何一个空白字符。相当于[f v]。

 S:任何一个非空白字符。相当于[^f v]。

8.+:匹配一个或多个字符。

 *:匹配0个或多个字符。

 ?:匹配1个或0个字符。

9.{3}:重复3次。

 {2,}重复2次以上。

10:懒惰型元字符:*?,+?。

var sToMatch="abbbaabbbaaabbb1234"
   var re1=/.*bbb/gi;
   var re2=/.*?bbb/g;
   var Match1=re1.exec(sToMatch);
   var Match2=re2.exec(sToMatch);
   alert(Match1);//abbbaabbbaaabbb
   alert(Match2);//abbb

11.表示边界。

例:var sToMatch1="abcd";
   var sToMatch2="bcd";
   var sToMatch3="bcde";
   var reBcd=/bcd/g;
   alert(reBcd.test(sToMatch1));//false
   alert(reBcd.test(sToMatch2));//ture
   alert(reBcd.test(sToMatch3));//false

12.^匹配一个字符串的开头,$匹配一个字符串的结尾。

13.|表示候选。

例:var sToMatch1="red";
   var sToMatch2="black";
   var reRedOrBlack=/(red|black)/;
   alert(reRedOrBlack.test(sToMatch1));//true
   alert(reRedOrBlack.test(sToMatch2));//true

14.new RegExp

注意点1:var pattern1=/[bc]at/gi;

  var pattern2=new RegExp["[bc]at","gi"];

  pattern1等价于pattern2。

注意点2:转义要进行双重转义。

注意点3: 通常转义为\,双重转义后为\\。

  

二.函数的使用。

1.test()。返回true或false。

例:var sToMatch="cat";
   var reCat=/cat/;//var reCat=new RegExp("cat","gi");也可用这种方式。
   alert(reCat.test(sToMatch));//true

2.search:返回在字符串中出现的一个匹配的位置。

例:var sToMatch="a bat, a Cat, a fAt baT, a faT cat";
   var reAt=/at/gi;
   alert(sToMatch.search(reAt));

3.replace:替换。

var sToChange="The sky is red";
   var reRed=/red/;
   alert(sToChange.replace(reRed,"blue"));//The sky is blue

4.split

var sColor="red,blue,yellow,green";
   var reComma=/\,/;
   var arrColors=sColor.split(reComma);
   for(var i=0;i<4;i++)
   {
      alert(arrColors[i]);
   }//一次输出red,blue,yellow,green。

5.trim().//去掉字符俩端的空格

String.prototype.trim=function()
   {
      var reExtraSpace=/^s+(.*?)s+$/;
      return this.replace(reExtraSpace,"$1");
   }
   var sTest="     This is a test         ";
   alert("["+sTest+"]");//[     This is a test         ]
   alert("["+sTest.trim()+"]");//[This is a test]

6.match().

var sToMatch="a bat, a Cat, a fAt baT, a faT cat";
   var reAt=/at/gi;
   var arrMatches=sToMatch.match(reAt);
   for(var i=0;i<6;i++)
   {
      alert(arrMatches[i]);
   }//at at At aT at

7.exec().

var sToMatch="a bat, a Cat, a fAt baT, a faT cat";
   var reAt=/at/;
   var arrMatches=reAt.exec(sToMatch);
   for(var i=0;i<6;i++)
   {
      alert(arrMatches);
   }//at at at at at at

8.比较match与exec

var sToMatch="a bat, a Cat, a fat bat, a fat cat";
var reAt=/(a)(t)/g;
var arrMatches=sToMatch.match(reAt);
for(var i=0;i<6;i++) {
    console.log(arrMatches[i]);//at, at, at, at, at, at
}

  

var sToMatch="a bat, a Cat, a fat bat, a fat cat";
var reAt=/(a)(t)/;
var arrMatches=sToMatch.match(reAt);
for(var i=0;i<6;i++) {
    console.log(arrMatches[i]);//at, a, t, undefined, undefined, undefined
}

  

var sToMatch="a bat, a Cat, a fat bat, a fat cat";
var reAt=/(a)(t)/;
var arrMatches=reAt.exec(sToMatch);
for(var i=0;i<6;i++) {
    console.log(arrMatches[i]);//at, a, t, undefined, undefined, undefined
}

  

var sToMatch="a bat, a Cat, a fat bat, a fat cat";
var reAt=/(a)(t)/g;
var arrMatches=reAt.exec(sToMatch);
for(var i=0;i<6;i++) {
    console.log(arrMatches[i]);//at, a, t, undefined, undefined, undefined
}
var sToMatch="a bAt, a Cat, a fat bat, a fat cat";
var reAt=/(a)(t)/ig;
var arrMatches=reAt.exec(sToMatch);
for(var i=0;i<6;i++) {
    console.log(arrMatches[i]);//At, A, t, undefined, undefined, undefined
}
var arrMatches=reAt.exec(sToMatch);
for(var i=0;i<6;i++) {
    console.log(arrMatches[i]);//at, a, t, undefined, undefined, undefined
}

1. 只有在正则表达式必须指定全局g属性时,match才能返回所有匹配,否则match与exec方法结果无差异,是等价的,全局下exec第二次执行是会匹配下一个。exec会返回匹配的位置信息。
2. exec永远返回与第一个匹配相关的信息,其返回数组第一个值是第一个匹配的字串,剩下的是所有分组的反向引用(即子括号的匹配内容,与全局(g)无关。

例:var someText= "web2.0 .net2.0" ;
var pattern=/(/w+)(/d)/.(/d)/g;
var outCome_exec=pattern.exec(someText);
var outCome_matc=someText.match(pattern);

exec执行后的最终结果是:["web2.0","web","2","0"]

outCome_matc为["web2.0","net2.0"]

三:模式:

1.反向引用

方式一:var sToMatch="#123456789";
   var reNumbers=/#(d+)/;
   renumbers.test(sToMatch);
   alert(RegExp.$1);//123456789

方式二:var sToMatch="dogdog";
   var reDogDog=/(dog)1/;
   alert(reDogDog.test(sToMatch));//true

2.前瞻:

var sToMatch1="bedroom";
   var sToMatch2="bedding";
   var reBed1=/(bed(?=room))/;
   var reBed2=/(bed(?!room))/;
   alert(reBed1.test(sToMatch1));//true
   alert(reBed1.test(sToMatch2));//false
   alert(reBed2.test(sToMatch1));//flase
   alert(reBed2.test(sToMatch2));//true

3.非捕获型分组

var sToMatch="#123456789";
   var reNumbers=/#(?:d+)/;
   alert(sToMatch.replace(reNumbers,"$1"));//$1

四:理解RegExp对象

1.实例属性

global:g是否已设置。

ignoreCase:i是否已设置。

multiline:i是否已设置。

source:正则表达式元源字符串形式。

lastIndex:整数,代表下次匹配将会从哪个字符位置开始(只有当使用exec()和test()函数才会填入,否则为0。

var reTest=/[ba]*/i
   alert(reTest.global);//false
   alert(reTest.ignoreCase);//true
   alert(reTest.multiline);//false
   alert(reTest.source);//[ba]*

var sToMatch="bbg is short for barbecue"
   var reB=/b/g;
   reB.exec(sToMatch);
   alert(reB.lastIndex);//1
   reB.exec(sToMatch);
   alert(reB.lastIndex);//2
   reB.exec(sToMatch);
   alert(reB.lastIndex);//18
   reB.exec(sToMatch);
   alert(reB.lastIndex);//21

2.静态属性:

input:等价于$_。

lastMatch:等价于$&。

lastParen等价于$+。

leftContext等价于$`。

multiline等价于$*。

rightContext等价于$'。

var sToMatch="this has been a short,short summmer";
   var reShort=/(s)hort/g;
   reShort.test(sToMatch);
   alert(RegExp.input);//this has been a short,short summmer
   alert(RegExp.leftContext);//this has been a
   alert(RegExp.rightContext);//,short summmer
   alert(RegExp.lastMatch);//short
   alert(RegExp.lastParen);//s
   alert(RegExp.$_);//this has been a short,short summmer
   alert(RegExp["$`"]);//this has been a
   alert(RegExp["$'"]);//,short summmer
   alert(RegExp["$&"]);//short
   alert(RegExp["$+"]);//s

原文地址:https://www.cnblogs.com/pcd12321/p/4528459.html