javascript - Chrome V8 Bug? Function Acting different after being called a 2nd time - Stack Overflow

Please take a look at the following JavaScript. I've taken stuff out of it, so you may focus on th

Please take a look at the following JavaScript. I've taken stuff out of it, so you may focus on the essence of the problem.

You'll notice that I call the prepPath function twice in a row, passing in the exact same string. In firefox and IE8, this function alerts true each time (as expected). But, in Chromium 5.0.375.127 (55887) Ubuntu 10.04, the function returns true the first time, and false the 2nd call, despite the input remaining exactly the same!

<script type="text/javascript"> 
    function prepPath(str)
    {   
        var regX = /[^\s/"'\\].*[^\s/"'\\]/g;
        if(regX.test(str))
        {
            alert("true: " + str);
        }
        else
        {
            alert("false; " + str);
        }
    }

    prepPath("/desktop"); // alerts: true
    prepPath("/desktop"); // alerts: false 
</script> 

Why is it returning false the second time in Chromium?

Please take a look at the following JavaScript. I've taken stuff out of it, so you may focus on the essence of the problem.

You'll notice that I call the prepPath function twice in a row, passing in the exact same string. In firefox and IE8, this function alerts true each time (as expected). But, in Chromium 5.0.375.127 (55887) Ubuntu 10.04, the function returns true the first time, and false the 2nd call, despite the input remaining exactly the same!

<script type="text/javascript"> 
    function prepPath(str)
    {   
        var regX = /[^\s/"'\\].*[^\s/"'\\]/g;
        if(regX.test(str))
        {
            alert("true: " + str);
        }
        else
        {
            alert("false; " + str);
        }
    }

    prepPath("/desktop"); // alerts: true
    prepPath("/desktop"); // alerts: false 
</script> 

Why is it returning false the second time in Chromium?

Share Improve this question asked Sep 13, 2010 at 18:00 Lonnie BestLonnie Best 11.5k14 gold badges66 silver badges109 bronze badges 1
  • 2 The only time you need the 'g' flag in a regular expresssion is if you want to use the same expression more than once, with the lastIndex flagged between uses. Without the 'g' flag your code works the way you would expect. If you set regX.lastIndex=0 after defining it itwill also return the expected value. – kennebec Commented Sep 13, 2010 at 18:47
Add a ment  | 

1 Answer 1

Reset to default 14

There's some ambiguity in the spec about when literal regexes should get reset (recall that they have state). You can work around this by doing this:

var regX = new RegExp(/[^\s/"'\\].*[^\s/"'\\]/g);

live example: http://jsbin./irate

or this:

var regX = /[^\s/"'\\].*[^\s/"'\\]/g;
regX.lastIndex = 0;

live example: http://jsbin./irate/2

I'm informed by those who've looked into it more than I have that it's not actually an outright bug, but an ambiguity. And it's not just Chrome, some versions of other browsers have also had a similar problem.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744770915a4592752.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信