Javascript 日期格式 正则表达式 理解语法

标签 javascript regex

我遇到了正则表达式引发的 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)  <em>goes to the first digit?</em>
    (
    (<br/>
        ?'year' *is this another holder/state? If so, why is it not, ?year:  ? *
        (
        (1[6-9])
        |
        ([2-9]\d)
        )<p></p>

<pre><code>        \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*
    )
)
?
</code></pre>

<p>)
$
</p>

最佳答案

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

关于Javascript 日期格式 正则表达式 理解语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16571520/

相关文章:

javascript - HTML - 带下拉菜单的跨页面导航栏(包含 PHP)?

c++ - 为什么这个正则表达式不适用于 c++ TR1?

javascript - 在 jinja 条件语句中创建的模态都显示相同的数据

javascript - 启用/禁用可排序取决于元素组

php - 奇怪的 CSS 加载

Java – 正则表达式 – 匹配开始和结束 curl 标签

regex - 正则表达式,允许在字符串中包含空格,但不仅限于空格

javascript - 将数组作为参数传递给函数以选择随机字符串

regex - 如何使用正则表达式负前瞻

正则表达式 - 用相同数量的另一个字符替换一个字符的序列