如何在JavaScript中使用这个正则表达式?

时间:2022-07-11 20:12:13

I am trying to use the following regular expression to test for a valid date.

我正在尝试使用下面的正则表达式来测试一个有效的日期。

^(((0?[1-9]|1[012])/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)/(19|[2-9]\d)\d{2}|0?2/29/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$

which I got from

这是我从

http://regexlib.com/REDetails.aspx?regexp_id=1071

http://regexlib.com/REDetails.aspx?regexp_id=1071

My JavaScript test code is:

我的JavaScript测试代码是:

var date='1/1/1965';
var re = new RegExp('^(((0?[1-9]|1[012])/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)/(19|[2-9]\d)\d{2}|0?2/29/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$');
alert(re.test(date));

I keep getting "false" rather than "true" for this valid test date.

对于这个有效的测试日期,我总是得到“false”而不是“true”。

3 个解决方案

#1


3  

Try this:

试试这个:

var date='1/1/1965'; 
var re = /^(((0?[1-9]|1[012])\/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])\/(29|30)|(0?[13578]|1[02])\/31)\/(19|[2-9]\d)\d{2}|0?2\/29\/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$/;
alert(re.test(date)); 

#2


0  

Your problem might be escaping of the slashes.

你的问题可能是逃避鞭打。

There are 2 ways you can insert a regexp in javascript. 1st way is just put it in forward slashes, and the second way, which is the way you are doing it is with RegExp object, which means that you then have to include the regexp string itself in the JavaScript code as a string.

有两种方法可以在javascript中插入regexp。第一种方法是将它放到斜线中,第二种方法是使用RegExp对象,这意味着必须将RegExp字符串本身作为字符串包含在JavaScript代码中。

And as every other string in Javascript which contains special characters like backward slashes (which your regexp does), you have to escape them with yet another backslash.

和Javascript中包含特殊字符(比如向后斜线)的其他字符串一样,您必须使用另一个反斜线来转义它们。

So basically just replace every backslash in your code with double backslash (\\) and you should be fine.

所以基本上只要用双反斜杠(\\ \)替换代码中的每一个反斜杠就可以了。

#3


0  

Just put it in forward-slashes:

把它打上斜线:

var re = /^(((0?[1-9]|1[012])\/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)\/(19|[2-9]\d)\d{2}|0?2/29/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$/

Now, while that'll get you a regular expression to use, I have to say that this may be one of the silliest applications of a regex ever. You'd be way better off just instantiating a JavaScript "Date" object and using it's surprisingly friendly API to check what you need to check.

现在,尽管这将使您能够使用正则表达式,但我不得不说这可能是regex最愚蠢的应用之一。您最好实例化一个JavaScript“Date”对象,并使用它异常友好的API检查需要检查的内容。

If you instantiate a JavaScript "Date" object with a bogus date (like Februrary 30), it'll just roll to the actual date that sort-of corresponds to the bogus date; in other words, it carries over the bogus days into the next month. Thus, if you just hold on to the month and day, and create a "Date" instance, you can know that if the month and day that the "Date" instance gives you when you ask are different from the ones you fed it, then the original date must have been unreal.

如果你用一个伪日期实例化一个JavaScript“日期”对象(如二月三十日),它就会滚动到与伪日期相对应的实际日期;换句话说,它把虚假的日子延续到下个月。因此,如果您只保留月和日,并创建一个“日期”实例,您就可以知道,如果您请求时“日期”实例给出的月和日与您输入的日期不同,那么原始日期一定是不真实的。

#1


3  

Try this:

试试这个:

var date='1/1/1965'; 
var re = /^(((0?[1-9]|1[012])\/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])\/(29|30)|(0?[13578]|1[02])\/31)\/(19|[2-9]\d)\d{2}|0?2\/29\/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$/;
alert(re.test(date)); 

#2


0  

Your problem might be escaping of the slashes.

你的问题可能是逃避鞭打。

There are 2 ways you can insert a regexp in javascript. 1st way is just put it in forward slashes, and the second way, which is the way you are doing it is with RegExp object, which means that you then have to include the regexp string itself in the JavaScript code as a string.

有两种方法可以在javascript中插入regexp。第一种方法是将它放到斜线中,第二种方法是使用RegExp对象,这意味着必须将RegExp字符串本身作为字符串包含在JavaScript代码中。

And as every other string in Javascript which contains special characters like backward slashes (which your regexp does), you have to escape them with yet another backslash.

和Javascript中包含特殊字符(比如向后斜线)的其他字符串一样,您必须使用另一个反斜线来转义它们。

So basically just replace every backslash in your code with double backslash (\\) and you should be fine.

所以基本上只要用双反斜杠(\\ \)替换代码中的每一个反斜杠就可以了。

#3


0  

Just put it in forward-slashes:

把它打上斜线:

var re = /^(((0?[1-9]|1[012])\/(0?[1-9]|1\d|2[0-8])|(0?[13456789]|1[012])/(29|30)|(0?[13578]|1[02])/31)\/(19|[2-9]\d)\d{2}|0?2/29/((19|[2-9]\d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00)))$/

Now, while that'll get you a regular expression to use, I have to say that this may be one of the silliest applications of a regex ever. You'd be way better off just instantiating a JavaScript "Date" object and using it's surprisingly friendly API to check what you need to check.

现在,尽管这将使您能够使用正则表达式,但我不得不说这可能是regex最愚蠢的应用之一。您最好实例化一个JavaScript“Date”对象,并使用它异常友好的API检查需要检查的内容。

If you instantiate a JavaScript "Date" object with a bogus date (like Februrary 30), it'll just roll to the actual date that sort-of corresponds to the bogus date; in other words, it carries over the bogus days into the next month. Thus, if you just hold on to the month and day, and create a "Date" instance, you can know that if the month and day that the "Date" instance gives you when you ask are different from the ones you fed it, then the original date must have been unreal.

如果你用一个伪日期实例化一个JavaScript“日期”对象(如二月三十日),它就会滚动到与伪日期相对应的实际日期;换句话说,它把虚假的日子延续到下个月。因此,如果您只保留月和日,并创建一个“日期”实例,您就可以知道,如果您请求时“日期”实例给出的月和日与您输入的日期不同,那么原始日期一定是不真实的。