javascript - 字符串match()位置对正则exec()的影响
ringa_lee
ringa_lee 2017-04-11 12:26:03
0
0
337
        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的值。。。。

ringa_lee
ringa_lee

ringa_lee

reply all(0)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!