JavaScript:正则表达式 分组2

继续上一篇的写,这篇复杂点。

分组+范围

var reg=/([abcd]bc)/g;
 var str="abcd bbcd cbcd dbcd";
 console.log(str.match(reg));
 console.dir(reg.exec(str));
 console.dir(reg.exec(str));
 console.dir(reg.exec(str));
 console.dir(reg.exec(str));

 var reg=/([abcd]bc)/;
 var str="abcd bbcd cbcd dbcd";
 console.log(str.match(reg));
 console.dir(reg.exec(str));

 var reg=/[abcd]bc/g;
 var str="abcd bbcd cbcd dbcd";
 console.log(str.match(reg));
 console.dir(reg.exec(str));
  console.dir(reg.exec(str));
   console.dir(reg.exec(str));

 var reg=/[abcd]bc/g;
 var str="cbcd dbcd";
 console.log(str.match(reg));
 console.dir(reg.exec(str));
 console.dir(reg.exec(str));
 console.dir(reg.exec(str));

 var reg=/[abcd]bc/;
 var str="abcd bbcd cbcd dbcd";
 console.log(str.match(reg));
 console.dir(reg.exec(str));

正则表达式中有中刮号时:

将中挂号范围内的任何一个元素与刮号外相结合,在匹配字符串

只要有一个是成功的,那么该字符串就是匹配这个正则表达式。

不是全局时,就返回第一个匹配的字符串内容。

全局时,返回的从lastIndex开始的匹配的字符串的内容,match方法返回所有匹配的字符串内容。

 var reg=/[abcd]bcw*/g;
 var str="cbcd dbcd cbcd dbcd";
 console.log(str.match(reg));
 console.dir(reg.exec(str));
 console.dir(reg.exec(str));
 console.dir(reg.exec(str));

console.log("baddad".match(/([bd]ad?)*/))  //baddad,dad

分组+分组(嵌套分组)

原文地址:https://www.cnblogs.com/hongdada/p/3372178.html