zoukankan      html  css  js  c++  java
  • 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

    分组+分组(嵌套分组)

  • 相关阅读:
    JavaScript变量和作用域
    遥感专业词汇
    linux修改文件所属用户和用户组
    当singleton Bean依赖propotype Bean,可以使用在配置Bean添加look-method来解决
    linux中的目录和文件的统计
    linux命令在文件中根据命令查找
    走进ELK原理
    nohub和重定向文件
    HashMap与TreeMap按照key和value排序
    List自定义排序
  • 原文地址:https://www.cnblogs.com/hongdada/p/3372178.html
Copyright © 2011-2022 走看看