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]bc\w*/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

 

分组+分组(嵌套分组)

 

 

posted @   hongdada  阅读(308)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示