zoukankan      html  css  js  c++  java
  • javascript中的exec与match方法

    1)在使用match方法时,如果不指定g属性,则与RegExp对象的exec方法可以等价,而不是只有一个元素的数组。

    举例:

    var str= "ahi" ; var exp=/a(hi)/;

    var arr1 = exp.exec(str); var arr2 = str.match(exp); alert(arr1);//结果:arr1.length==2;arr1[0]==ahi;arr1[1]==hi; alert(arr2);//结果:arr2.length==2;arr2[0]==ahi;arr1[1]==hi;结果同上

    2)同时,在js帮助文档中,在执行exec方法时,如果有属性g,将该对象的匹配的开始位置设置到紧接这匹配子串的字符位置,当第二次调用exec时,将从 lastIndex所指示的字符位置开始检索。利用这个特点可以反复调用exec遍历所有匹配,此时等价于match具有g属性的情况(其实就是将匹配的结果放入Matches 集合中去了)。

    举例如下:

    a)有属性g的情况时,更新了index和lastIndex,对下次检索起到作用:

    function RegExpTest() {     var src = "The rain in Spain falls mainly in the plain.";     var re = /(\w+)/g; // 创建正则表达式模式。         var arr;     while ((arr = re.exec(src)) != null){      document.write(arr.index + "-" + RegExp.lastIndex + "\t" + arr[0]);//此处RegExp.lastIndex和arr.lastIndex均有同样的属性,可以互换。在此注意IE6和7的lastIndex重设置0的bug

        } };

    RegExpTest();

    //以上例子可以遍历所匹配的内容。并可得到每个小匹配的index和lastIndex;

    b)如果以上例子没有g的情况,则以上例子,exec方法没有更新RegExp 对象的全局属性(index、lastIndex等),以上例子会陷入死循环,index和lastIndex一直为0和3

    可见属性g在exec过程中可以改变index和lastIndex等的值,以便下一次检索的位置,match方法无此能力。

    关于index和lastIndex等属性(帮助中还有leftContext、rightContext、lastMatch、lastParen(最后一个括号),但是这些属性均以index和lastindex为基础)。

    1)只读属性。

    如下例子:

        var src = "The rain in Spain falls mainly in the plain.";     var re = /(\w+)/g; // 创建正则表达式模式。        var arr;     arr = re.exec(src);         RegExp.lastIndex = 0;     RegExp.index = 0;     arr.lastIndex = 0;     arr.index = 0;

        document.write(arr.index + "-" + arr.lastIndex + "\t" + arr[0]+"**********"+RegExp.index + "-" + RegExp.lastIndex + "\t" + arr[0]);

        //结果为0-0 The**********0-3 The。

    究其原因也就是RegExp的属性是只读的,即使js语言的灵活性,可以修任何属性或添加任何属性,均不报语法错误。但是依旧无法RegExp的属性更改,但是arrary对象则是可以更改,但是每次执行一次exec,就会将RegExp.index等属性重新赋值给返回的Arrary对象。

    例如:

    var src = "The rain in Spain falls mainly in the plain."; var re = /(\w+)/g; // 创建正则表达式模式。  var arr; arr = re.exec(src);  RegExp.lastIndex = 0; RegExp.index = 0; arr.lastIndex = 0; arr.index = 0;

    document.write(arr.index + "-" + arr.lastIndex + "\t" + arr[0]+"**********"+RegExp.index + "-" + RegExp.lastIndex + "\t" + arr[0]);

    //执行第二次arr的index属性会被更新,其实是RegExp对象实例在执行exec方法时,更新全局的RegExp.index和arr的index等,在后边会介绍

    arr = re.exec(src); document.write("<br/>"+arr.index + "-" + arr.lastIndex + "\t" + arr[0]+"**********"+RegExp.index + "-" + RegExp.lastIndex + "\t" + arr[0]);

    //0-0 The**********0-3 The //4-8 rain**********4-8 rain

    2)不同的RegExp实例对象交叉执行exec时,index、lastIndex等属性互不影响。每次执行exec或者执行String的match方法时,都会给RexExp.index等赋予新值。(这个其实是必须的,只是我在这脑袋一犯浑,给理解错了,主要是因为“RegExp.lastIndex = 0;”可以被赋值,但是取值时,结果又没有改变,让我脑袋混乱了。)

    开始我以为如果两个RegExp对象在交叉执行exec时,可能index等会清零。因为我认为index属性是保存在RegExp的全局静态属性上的。现在发现是保存在具体的RegExp实例上,每次执行exec或者执行String的match方法时,都会给RexExp.index等赋予新值。

    呵呵,这可能是习惯了c和java中类和类实例的想法的人常犯的错误,认为RegExp是个类,RegExp.index是一个类的static属性。这样认为没错,但是他的值是是会在执行exec和String的match方法时,被正则对象更新。

    举例如下:

        var src = "The rain in Spain falls mainly in the plain.";             var re1 = /(\w+)/; // 创建正则表达式模式。      var re2 = /(\w+)/g; // 创建正则表达式模式。      var arr;

        arr = re1.exec(src);         document.write("R1第一次执行exec:"+RegExp.index + "-" + RegExp.lastIndex + "\t" + arr[0]);          arr = re2.exec(src);     document.write("<br/>R2第一次执行exec:"+RegExp.index + "-" + RegExp.lastIndex + "\t" + arr[0]);          arr = re1.exec(src);     document.write("<br/>R1第二次执行exec:"+RegExp.index + "-" + RegExp.lastIndex + "\t" + arr[0]);

        arr = re2.exec(src);     document.write("<br/>R2第二次执行exec:"+RegExp.index + "-" + RegExp.lastIndex + "\t" + arr[0]);

    输出的结果如下:

    R1第一次执行exec:0-3 The R2第一次执行exec:0-3 The R1第二次执行exec:0-3 The R2第二次执行exec:4-8 rain

    3)String对象的match方法,无法像exec方法那样获取中间查找的对象的index和lastIndex,也就是说是一次性的。即无法得到下一次检索的位置,match方法在设置g属性时,只能获取最后一个检索和index和lastIndex;match在没有设置g属性时,仅仅获得第一个匹配的index和lastIndex。

    举例如下:

    a)

    var src = "The rain in Spain falls mainly in the plain."; var re = /\w+/g; //有g属性。  var i = 0; while (i++<10){     arr = src.match(re);     document.write(RegExp.index + "-" + RegExp.lastIndex + "\t" + arr + "<br/>");

    }

    //结果如下:

    38-43 The,rain,in,Spain,falls,mainly,in,the,plain 38-43 The,rain,in,Spain,falls,mainly,in,the,plain 38-43 The,rain,in,Spain,falls,mainly,in,the,plain 38-43 The,rain,in,Spain,falls,mainly,in,the,plain 38-43 The,rain,in,Spain,falls,mainly,in,the,plain 38-43 The,rain,in,Spain,falls,mainly,in,the,plain 38-43 The,rain,in,Spain,falls,mainly,in,the,plain 38-43 The,rain,in,Spain,falls,mainly,in,the,plain 38-43 The,rain,in,Spain,falls,mainly,in,the,plain 38-43 The,rain,in,Spain,falls,mainly,in,the,plain

    b)

    var src = "The rain in Spain falls mainly in the plain."; var re = /\w+/; // 无g属性。  var i = 0; while (i++<10){     arr = src.match(re);     document.write(RegExp.index + "-" + RegExp.lastIndex + "\t" + arr + "<br/>");

    } //结果如下:

    0-3 The 0-3 The 0-3 The 0-3 The 0-3 The 0-3 The 0-3 The 0-3 The 0-3 The 0-3 The

    c)

    var src = "The rain in Spain falls mainly in the plain."; var re = /\w+/g;  var i = 0; arr = src.match(re); while (arr[i]!=null){     document.write(RegExp.index + "-" + RegExp.lastIndex + "\t" + arr[i] + "<br/>");     i++; }  //结果如下:

    38-43 The 38-43 rain 38-43 in 38-43 Spain 38-43 falls 38-43 mainly 38-43 in 38-43 the 38-43 plain

    最后结论(如有不对,请指正):

    1)exec是RegExp对象方法,match是String对象方法;

    2)如果没有找到结果,则二者都返回null;

    3)只有在正则表达式必须指定全局g属性时,match才能返回所有匹配,否则match与exec方法结果无差异,是等价的;

    4)exec永远返回与第一个匹配相关的信息,其返回数组第一个值是第一个匹配的字串,剩下的是所有分组的反向引用(即子括号的匹配内容);

    5)exec在设置g属性后,虽然匹配结果不受g的影响,返回结果仍然是一个数组(第一个值是第一个匹配到的字符串,以后的为分组匹配内容),但是会改变index和lastIndex等的值,将该对象的匹配的开始位置设置到紧接这匹配子串的字符位置,当第二次调用exec时,将从lastIndex所指示的字符位置开始检索。同样match方法在设置了g属性后,也会改变index和lastIndex的值,但是是一次性的。无法像exec那样能逐过程累积(即将结果放入Matches 集合中去了),因此无法累积获取下一次检索的位置。

  • 相关阅读:
    C++实现多项式曲线拟合--polyfit-超定方程
    C# XmlDocument操作XML
    C#下使用XmlDocument详解
    前端常见的9种设计模式
    前端常用的设计模式
    前端需要了解的9种设计模式
    TCP协议详解
    请UI小姐姐喝了一杯奶茶要来的网站
    nodemon 基本配置与使用
    wireshark抓包新手使用教程
  • 原文地址:https://www.cnblogs.com/tianguook/p/3002009.html
Copyright © 2011-2022 走看看