美文网首页
5.正则捕获的懒惰性及解决方案

5.正则捕获的懒惰性及解决方案

作者: 追马的时间种草 | 来源:发表于2019-10-17 13:40 被阅读0次

上一篇:常用正则表达式


正则捕获的懒惰性及解决方案

  • 正则捕获

    ​ :把一个字符串和正则匹配的部分获取到

    ​ [正则方法:]

    ​ exec:捕获

    ​ test:匹配

    ​ [字符串方法实现正则捕获:]

    ​ replace

    ​ split

    ​ match

    ​ ……

  • 基于EXEC可以实现正则的捕获

    1. 如果当前正则和字符串不匹配,捕获的结果是null

    2. 如果匹配捕获的结果就是一个数组

      1. 索引0:大正则捕获的内容
      2. index:正则捕获的起始索引
      3. input:原始操作的字符串
      let str='shang2019xiaolin2020'
      let str1='shangxiaolin'
      let reg=/\d+/
      console.log(reg.test(str))//true
      console.log(reg.test(str1))//false
      //正则EXEC捕获
      console.log(reg.exec(str))//["2019", index: 5, input: "shang2019xiaolin2020", groups: undefined]
      console.log(reg.exec(str1))//null
      
  1. 执行一次exec只能捕获到一个和正则匹配的内容,其余内容还没有捕获到(执行多次,也没有卵用,依然只会捕获相同的内容)

    //正则EXEC捕获
    console.log(reg.exec(str))//["2019", index: 5, input: "shang2019xiaolin2020", groups: undefined]
    //正则EXEC捕获
    console.log(reg.exec(str))//["2019", index: 5, input: "shang2019xiaolin2020", groups: undefined]
    
  1. 正则的捕获天生具备懒惰性:只能捕获第一个匹配的内容,剩余的默认捕获不到(解释了上述3)

    • 懒惰性的原因:

      1. lastIndex的值没有变:正则在捕获的时候,lastIndex为下一次在字符串中开始查找的索引

        //=>LAST-INDEX的值不变,导值懒惰性
        console.log(reg.lastIndex)//=>0
        console.log(reg.exec(str))//["2019", index: 5, input: "shang2019xiaolin2020", groups: undefined]
        console.log(reg.lastIndex)//=>0
        console.log(reg.exec(str))//["2019", index: 5, input: "shang2019xiaolin2020", groups: undefined]
        
      2. 即使我们手动修改lastIndex,然而也没有卵用

        reg.lastIndex=9
        console.log(reg.lastIndex)//=>9
        console.log(reg.exec(str))//["2019", index: 5, input: "shang2019xiaolin2020", groups: undefined]
        
  • 解决正则懒惰性给正则加修饰符g

    • 加G是唯一解决方案,否则永远不能够全局匹配捕获
    let str='shang2019xiaolin2020'
    //加修饰符g
    let reg=/\d+/g; //g: 全局捕获
    console.log(reg.lastIndex)//0
    console.log(reg.exec(str))["2019", index: 5, input: "shang2019xiaolin2020", groups: undefined]
    console.log(reg.lastIndex)//9
    console.log(reg.exec(str))["2020", index: 5, input: "shang2019xiaolin2020", groups: undefined]
    console.log(reg.lastIndex)//20
    console.log(reg.exec(str))//null
    console.log(reg.lastIndex)//0
    console.log(reg.exec(str))["2019", index: 5, input: "shang2019xiaolin2020", groups: undefined]
    
  • match()捕获:字符方法:只需一次执行就可以捕获所有匹配的内容)

    let str='shang2019xiao2020lin2021'
    let reg=/\d+/g;
    str.match(reg)// ["2019", "2020", "2021"]
    
  • 封装(match()原理)

    需求:原: 全局捕获想要捕获到所有匹配到的内容需一次一次的执行exec(太麻烦)
    现: 我们需封装一个只需一次执行就可以捕获所有匹配的内容的程序

    let str='shang2019xiao2020lin2021'
    let reg=/\d+/g;
    RegExp.prototype.myExecAll=functioin(str){
        //this: reg(当前操作的正则)
        //str:传入的需要捕获的字符串
        //执行EXEC开始捕获,具体捕获多少次不定,但是一直到捕获不到内容(null)为止,期间把捕获到的内容存储到数组中即可
           if(!this.global){//=>为防止出出现死循环,我们需要检测一下正则是否加G,没有加G只需捕获一次返回结果即可
                //global:正则属性,加'G'global为true.没有'G'global为false
               return this.exec(str)
            }
        let result=[];
        let valAry=this.exec(str);
        while(valAry){
            result.push(valAry[0]);//把每一次捕获到的结果第一项存储到容器中
            valAry=this.exec(str);
        }
        return result
    };
    console.log(reg.myExecAll(str))//["2019", "2020", "2021"]
    

下一篇:正则捕获的贪婪性及解决方案

相关文章

网友评论

      本文标题:5.正则捕获的懒惰性及解决方案

      本文链接:https://www.haomeiwen.com/subject/nubmmctx.html