首页 > 字符串match()位置对正则exec()的影响

字符串match()位置对正则exec()的影响

        var str = 'abc rbc fbc dbc';
        var reg = /.(bc)/g;
        var match1 = reg.exec(str);
        var match2 = str.match(reg);
        console.log(match1);
        console.log(reg.lastIndex);
        console.log(match1.index)
        match1 = reg.exec(str);
        console.log(match1);
        console.log(reg.lastIndex);
        console.log(match1.index)
        match1 = reg.exec(str);
        console.log(match1);
        console.log(reg.lastIndex);
        console.log(match1.index);
        //var match2 = str.match(reg);
        console.log(match2);

代码所示,我将var match2 = str.match(reg);的位置改变,输出的值是不一样的。

/5分钟之后更新/
哦哦,我傻了,match()相当于又执行了一次exec(),当然会改变lastIndex的值。。。。

【热门文章】
【热门文章】