我遇到了一个由正则表达式引发的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
我花了无数个小时试图找出这个表达式中的错误。但是,我找不到关于某些语法的任何文档。这里是我到目前为止所拥有的,任何帮助或指点我的正确方向将是非常感谢的!
^(?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*
)
)
?
)
$
发布于 2013-05-15 17:40:43
使用此正则表达式1>Get日期
^(\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
https://stackoverflow.com/questions/16571520
复制相似问题