我的约会正则表达式出了什么问题? [重复]

时间:2022-04-18 03:40:30

This question already has an answer here:

这个问题在这里已有答案:

var dateRegex = /\/Date\((\d+)\)\//g;    // [0-9] instead of \d does not help.
dateRegex.test("/Date(1286443710000)/"); // true
dateRegex.test("/Date(1286445750000)/"); // false

Both Chrome and Firefox JavaScript consoles confirm. What the hell, guys?

Chrome和Firefox JavaScript控制台都证实了这一点。怎么了,伙计们?

Edit: even simpler test case:

编辑:更简单的测试用例:

var dateRegex = /Date\(([0-9]+)\)/g;
dateRegex.test("Date(1286445750000)"); // true
dateRegex.test("Date(1286445750000)"); // false
dateRegex.test("Date(1286445750000)"); // true
dateRegex.test("Date(1286445750000)"); // false
dateRegex.test("Date(1286445750000)"); // true

This shows that it alternates true/false every time...

这表明它每次都交替出现真/假......

2 个解决方案

#1


21  

In your case remove the g modifier from the end, for example:

在您的情况下,从末尾删除g修饰符,例如:

var dateRegex = /\/Date\((\d+)\)\//;
dateRegex.test("Date(1286445750000)"); // true
dateRegex.test("Date(1286445750000)"); // true
dateRegex.test("Date(1286445750000)"); // true
dateRegex.test("Date(1286445750000)"); // true
dateRegex.test("Date(1286445750000)"); // true

It's a bug with the way regexes are implemented in ECMAScript 3, there's a great post on the details here.

这是在ECMAScript 3中实现正则表达式的方式的错误,这里有一篇很好的帖子。

#2


1  

The /g was causing problem. Following code works fine.

/ g引起了问题。以下代码工作正常。

<div id="test"></div>
    <script type="text/javascript">
        var reg = /Date\(\d+\)/; //REGEX WITHOUT g
        var d="Date(1286445750000)";
        $(function(){
            var $d=$("div#test");
            for(var i=0;i<100;i++){
                if(reg.test(d)){
                    $d.html($d.html()+"<br/>Matched: ["+d+"]");
                }
                else{
                    $d.html($d.html()+"<br/>Not Matched: ["+d+"]");
                }
            }
        });
    </script>

#1


21  

In your case remove the g modifier from the end, for example:

在您的情况下,从末尾删除g修饰符,例如:

var dateRegex = /\/Date\((\d+)\)\//;
dateRegex.test("Date(1286445750000)"); // true
dateRegex.test("Date(1286445750000)"); // true
dateRegex.test("Date(1286445750000)"); // true
dateRegex.test("Date(1286445750000)"); // true
dateRegex.test("Date(1286445750000)"); // true

It's a bug with the way regexes are implemented in ECMAScript 3, there's a great post on the details here.

这是在ECMAScript 3中实现正则表达式的方式的错误,这里有一篇很好的帖子。

#2


1  

The /g was causing problem. Following code works fine.

/ g引起了问题。以下代码工作正常。

<div id="test"></div>
    <script type="text/javascript">
        var reg = /Date\(\d+\)/; //REGEX WITHOUT g
        var d="Date(1286445750000)";
        $(function(){
            var $d=$("div#test");
            for(var i=0;i<100;i++){
                if(reg.test(d)){
                    $d.html($d.html()+"<br/>Matched: ["+d+"]");
                }
                else{
                    $d.html($d.html()+"<br/>Not Matched: ["+d+"]");
                }
            }
        });
    </script>