zoukankan      html  css  js  c++  java
  • string.match(RegExp) 与 RegExp.exec(string) 深入详解

    string.match(RegExp) 与 RegExp.exec(string) 相同点与不同点对比解析:

    1. 这两个方法,如果匹配成功,返回一个数组,匹配失败,返回null。

    2. 当RegExp的global属性为false时,这两个方法的返回数组是一样的。

    数组的第0个元素是整个str的第一个匹配字符串,接下来的元素是str第一个匹配中的子匹配字符串。

      此外,数组还有index和input两个额外属性,index是匹配字符串的起始位置,input是整个输入字符串。

      此时,RegExp的lastIndex属性一直是0(可为什么跟下面测试结果不一样呢?)。

    实例01(不带g标识符):

     1 <script type="text/JavaScript">                                    
     2                                                                    
     3 var str="this is a string";                                        
     4                                                                    
     5 var reg=/w*(i)s/;                                             
     6                                                                    
     7 var rm=str.match(reg);                                             
     8                                                                    
     9 var re=reg.exec(str);                                              
    10                                                                    
    11 document.write("string.match(RegExp)测试结果:<br>");             
    12                                                                    
    13 document.write("string.match(RegExp)返回数组:"+rm+"<br>");       
    14                                                                    
    15 document.write("string.match(RegExp).index:"+rm.index+"<br>");    
    16                                                                    
    17 document.write("string.match(RegExp).input:"+rm.input+"<br>");
    18 
    19 document.write("string.match(RegExp).lastIndex:"+reg.lastIndex+"<br>");    
    20                                                                 
    21 document.write("===============================<br>");            
    22                                                                    
    23 document.write("RegExp.exec(string)测试结果:<br>");              
    24                                                                    
    25 document.write("RegExp.exec(string)返回数组:"+re+"<br>");        
    26                                                                    
    27 document.write("RegExp.exec(string).index:"+re.index+"<br>");    
    28                                                                    
    29 document.write("RegExp.exec(string).input:"+re.input+"<br>");    
    30 
    31 document.write("RegExp.exec(string).lastIndex:"+reg.lastIndex+"<br>");    
    32 
    33 </script>   
    34 
    35 输出结果:
    36 
    37 string.match(RegExp)测试结果:
    38 
    39 string.match(RegExp)返回数组:this,i
    40 
    41 string.match(RegExp).index:0
    42 
    43 string.match(RegExp).input:this is a string
    44 
    45 string.match(RegExp).lastIndex:4
    46 
    47 ===============================
    48 
    49 RegExp.exec(string)测试结果:
    50 
    51 RegExp.exec(string)返回数组:this,i
    52 
    53 RegExp.exec(string).index:0
    54 
    55 RegExp.exec(string).input:this is a string
    56 
    57 RegExp.exec(string).lastIndex:4
    58 
    59                                   
    代码01

    3. 当RegExp的global属性为true时,返回的数组是不同的。

      match()方法返回的数组包含着所有匹配字符串,没有子匹配字符串和额外属性(为什么下面实际测试是有index和input额外属性的呢?而且index的数值是最后一个匹配字符串的位置?)。此时,lastIndex属性无效。

      exec()方法返回的数组格式与global为false时一样,只是此时RegExp的lastIndex属性有效,匹配是从lastIndex所指示的字符开始的,并且方法执行后会将lastIndex置为本次匹配

      字符串的下一个字符处,所以循环执行exec方法时会依次匹配整个字符串,直到字符串最后返回null,并将lastIndex置0。

          注:下面的测试代码必须设置两个RegExp变量(reg1,reg2),否则re始终为null,while(){}循环内部始终进不去,至于原因,暂时不知道!!!!!!

     实例02(带g标识符):

     1 <script type="text/JavaScript">
     2 var str="this is a string";
     3 var reg1=/w*(i)s/g;
     4 var reg2=/w*(i)s/g;
     5 var rm = str.match(reg1);
     6 var re;
     7 document.write("string.match(RegExp)带全局变量g 测试结果:<br>");
     8 document.write("string.match(RegExp)返回数组:"+rm+"<br>");
     9 document.write("string.match(RegExp).index:"+rm.index+"<br>");
    10 document.write("string.match(RegExp).input:"+rm.input+"<br>");
    11 document.write("string.match(RegExp).lastIndex:"+reg1.lastInde+"<br>");
    12 
    13 document.write("==========================================<br>");
    14 document.write("RegExp.exec(string)带全局变量g 测试结果:<br>");
    15 while(re=reg2.exec(str))
    16 {
    17  document.write("RegExp.exec(string)返回数组:"+re+"<br>");
    18  document.write("RegExp.exec(string).index:"+re.index+"<br>");
    19  document.write("RegExp.exec(string).input:"+re.input+"<br>");
    20  document.write("RegExp.exec(string).lastIndex:"+reg2.lastIndex+"<br>");
    21  document.write("----------------------------------------<br>");
    22 
    23 }
    24 document.write("RegExp.exec(string)循环完成后返回数组:"+re+"<br>");
    25 document.write("RegExp.exec(string).lastIndex:"+reg2.lastIndex+"<br>");
    26 
    27 </script>
    28 
    29 输出结果:
    30 
    31 string.match(RegExp)带全局变量g 测试结果:
    32 string.match(RegExp)返回数组:this,is
    33 string.match(RegExp).index:5
    34 string.match(RegExp).input:this is a string
    35 string.match(RegExp).lastIndex:undefined
    36 ==========================================
    37 RegExp.exec(string)带全局变量g 测试结果:
    38 RegExp.exec(string)返回数组:this,i
    39 RegExp.exec(string).index:0
    40 RegExp.exec(string).input:this is a string
    41 RegExp.exec(string).lastIndex:4
    42 ----------------------------------------
    43 RegExp.exec(string)返回数组:is,i
    44 RegExp.exec(string).index:5
    45 RegExp.exec(string).input:this is a string
    46 RegExp.exec(string).lastIndex:7
    47 ----------------------------------------
    48 RegExp.exec(string)循环完成后返回数组:null
    49 RegExp.exec(string).lastIndex:0
    代码02

    综上:
    1.在没有g标识符时,match和exec方法效果是一样的;有g标识符时,exec方法可以提供最完整的匹配结果。
    2.这里顺便提一下RegExp.test()方法,它是exec方法的简化版,有匹配结果就返回true,没有匹配结果就返回false,执行过程与exec是一样的。相当于 (p.exec(s) != null)。
    3.RegExp的lastIndex属性在有g标识符,且在exec和test方法中是有效的,其他地方是无效的(可实际上在string.match(RegExp)不带g标识符的方法中,也是有效的,参看上面 代码01)。

    热心技术,并兼吃喝,偶谈风月,不言国事.
  • 相关阅读:
    洛谷P2569 (BZOJ1855)[SCOI2010]股票交易 【单调队列优化DP】
    洛谷 P2254 [NOI2005]瑰丽华尔兹(单调栈优化DP)
    CF372C Watching Fireworks is Fun(单调队列优化DP)
    2019牛客全国多校第八场A题 All-one Matrices(单调栈)
    HDU3896 Greatest TC(双联通分量+倍增)
    2019牛客多校第7场
    ZOJ 2112 Dynamic Rankings(树状数组+主席树)
    2019 杭电多校第六场 题解
    HDU2242 考研路茫茫——空调教室 (双联通分+树形DP)
    HDU5536 Chip Factory
  • 原文地址:https://www.cnblogs.com/baby-zhude/p/4126600.html
Copyright © 2011-2022 走看看