1

我遇到了一个正则表达式引发的 Javascript 运行时错误。正则表达式验证日期格式。接受 (yyyy/mm/dd) 拒绝(mm/dd/yyyy) 等....我已经阅读了多个关于 javascript 正则表达式的站点

http://www.javascriptkit.com/javatutors/redev2.shtml http://www.javascriptkit.com/javatutors/re2.shtml http://www.diveintojavascript.com/articles/javascript-regular-expressions http:// /www.w3schools.com/jsref/jsref_obj_regexp.asp

我花了无数个小时试图找出这个表达式中的错误。但是我找不到有关某些语法的任何文档。这是我到目前为止所拥有的,任何帮助或指出我正确的方向将不胜感激!

^(?ni:(?=\d)((?'year'((1[6-9])|([2-9]\d))\d\d)(?'sep'[/])(?'month'0?[1-9]|1[012])\2(?'day'((?<!(\2((0?[2469])|11)\2))31)|(?<!\2(0?2)\2)(29|30)|((?<=((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(16|[2468][048]|[3579][26])00)\2\3\2)29)|((0?[1-9])|(1\d)|(2[0-8])))(?:(?=\x20\d)\x20|$))?((?<time>((0?[1-9]|1[012])(:[0-5]\d){0,2}(\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2}))?)$

我添加了空格和问题。我所有问题的开头和结尾都有 *。如果您还可以告诉我为什么这会导致运行时错误,那也太棒了!


^
(
    ?ni:    * What does ?ni: mean? Is it a holder of some sort?*
    (?=\d)  goes to the first digit?
    (
    (
?'year' *is this another holder/state? If so, why is it not, ?year: ? * ( (1[6-9]) | ([2-9]\d) )

        \d\d  *consumes the next two digits?*
)
(?'sep'[/]) *puts the seprator into sep?*
(?'month'0?[1-9]|1[012]) 
\2 *no idea what this does. Does it go back to start of text?*
(   
    ?'day'  *no idea what this does*
    (
            (
                ?<! *no idea what this does*
        (
             \2     
         (
             (0?[2469])
             |
             11
             )
             \2  
        )
    )   
    31
    )
    |
    (?<!\2(0?2)\2) *no idea what this does*
        (29|30)
        |
        (
            (
                ?<=    *no idea what this does*
                (
                    (
                        1[6-9]|[2-9]\d
                    )
                    (
                        0[48]
                        |
                        [2468][048]
                        |
                        [13579][26]
                    )
                    |
                    (16|[2468][048]|[3579][26])
                    00
                )
                \2  *no idea what this does*
                \3  *no idea what this does*
                \2  *no idea what this does*
            )
            29
        )
        |
        (
            (0?[1-9])
            |
            (1\d)
            |
            (2[0-8])
        )
    )
    (
        ?:   
        (?=\x20\d)\x20   *why is this using \x20 instead of \s?*
        |
        $  *no idea what this does... Does this finish the expression?*
    )
)
?
(
    (
        ?
        <time> *no idea what this does*
        (
            (
                0?
                [1-9]
                |
                1[012]
            )
            (:[0-5]\d){0,2}
            (\x20[AP]M)
        )
        |
        (
            [01]\d
            |
            2[0-3]
        )
        (:[0-5]\d){1,2} *no idea what this does*
    )
)
?

) $

4

1 回答 1

1

1>使用这个正则表达式获取日期

^(\d{4})/(\d{2})/(\d{2})$

2> 如果字符串与上述正则表达式匹配,则验证月、年、日!

var match = myRegexp.exec(myString);
parseInt(match[0],10);//year
parseInt(match[1],10);//month
parseInt(match[2],10);//day
于 2013-05-15T17:40:43.250 回答