首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Javascript正则表达式理解语法

Javascript正则表达式理解语法
EN

Stack Overflow用户
提问于 2013-05-15 17:37:35
回答 1查看 180关注 0票数 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 regexp.asp

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

代码语言:javascript
运行
复制
^(?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}))?)$

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

代码语言:javascript
运行
复制
^
(
    ?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*
    )
)
?


)
$
EN

回答 1

Stack Overflow用户

发布于 2013-05-15 17:40:43

使用此正则表达式1>Get日期

代码语言:javascript
运行
复制
^(\d{4})/(\d{2})/(\d{2})$

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

代码语言:javascript
运行
复制
var match = myRegexp.exec(myString);
parseInt(match[0],10);//year
parseInt(match[1],10);//month
parseInt(match[2],10);//day
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16571520

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档