我遇到了正则表达式抛出的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*
)
)
?
)
$
答案 0 :(得分:1)
1&gt;使用此正则表达式获取日期
^(\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