Javascript Regex-不介意点菜吗?
我正在尝试创建一个可以接受常规SQL/Js日期的正则表达式,例如: 2012年2月31日 我已经做了一个正则表达式:Javascript Regex-不介意点菜吗?,javascript,regex,Javascript,Regex,我正在尝试创建一个可以接受常规SQL/Js日期的正则表达式,例如: 2012年2月31日 我已经做了一个正则表达式: (0[1-9]|[12]\d|3[01])[ ]+(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)[ ]+[12]\d{3}$ 但是我如何告诉正则表达式忽略项目的外观顺序: 因此,它可以接受: Apr 31 2010 2010 Apr 31 ... ... 您应该将字符集中到组中,然后可以使用或接受它们在字符
(0[1-9]|[12]\d|3[01])[ ]+(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)[ ]+[12]\d{3}$
但是我如何告诉正则表达式忽略项目的外观顺序:
因此,它可以接受:
Apr 31 2010
2010 Apr 31
...
...
您应该将字符集中到组中,然后可以使用或接受它们在字符串中出现的不同顺序。您不能构建一个可以处理任何顺序的通用正则表达式-您必须清楚地指定它们出现的所有顺序。您应该将字符集中到组中,然后您可以使用或接受它们在字符串中出现的不同顺序。您无法构建可以处理任何顺序的通用正则表达式-您必须清楚地指定它们发生的所有顺序。一个使用前瞻断言的解决方案:
var myregexp = /^(?=.*(\b[A-Za-z]{3}\b))(?=.*(\b\d{1,2}\b))(?=.*(\b\d{4}\b))/;
var match = myregexp.exec(subject);
if (match != null) {
month = match[1];
days = match[2];
year = match[3];
}
说明:
^ # Start of string
(?= # Look ahead to see if the following can be matched:
.* # Any number of characters
( # followed by (capturing this in group no. 1)
\b # Start of word
[A-Za-z]{3} # Three ASCII letters
\b # End of word
) # End of capturing group no. 1
) # End of lookahead assertion.
(?= # Look ahead to see if the following can be matched:
.* # Any number of characters
( # followed by (capturing this in group no. 1)
\b\d{1,2}\b # a one- or two-digit number
) # etc.
)
(?= # Lookahead no. 3
.*
(
\b\d{4}\b # a four-digit number
)
)
一种使用前瞻断言的解决方案:
var myregexp = /^(?=.*(\b[A-Za-z]{3}\b))(?=.*(\b\d{1,2}\b))(?=.*(\b\d{4}\b))/;
var match = myregexp.exec(subject);
if (match != null) {
month = match[1];
days = match[2];
year = match[3];
}
说明:
^ # Start of string
(?= # Look ahead to see if the following can be matched:
.* # Any number of characters
( # followed by (capturing this in group no. 1)
\b # Start of word
[A-Za-z]{3} # Three ASCII letters
\b # End of word
) # End of capturing group no. 1
) # End of lookahead assertion.
(?= # Look ahead to see if the following can be matched:
.* # Any number of characters
( # followed by (capturing this in group no. 1)
\b\d{1,2}\b # a one- or two-digit number
) # etc.
)
(?= # Lookahead no. 3
.*
(
\b\d{4}\b # a four-digit number
)
)
要添加此答案:您可以为每个组件(日期、月、年)创建具有正则表达式的变量,然后通过使用
|
以可接受的顺序指定它们来创建正则表达式。e、 g.“(“+DAY+”+“+”+“+”+MONTH+“+”+“+”YEAR+“+”+“+”+MONTH+“+”+“+”+DAY+””)
在这个答案中添加:您可以为每个组件(日期、月份、年份)创建具有正则表达式的变量,然后通过使用
以可接受的顺序指定它们来创建正则表达式。e、 g.“(“+DAY+”+“+”+月+“+”+年+“+”+“+”+月+“+”+日+”)
这仍然是一个常规(严格地说)表达式,顺便说一句:)@蒂姆:这些文字评论是你自己写的,还是solftware写的?哪一个?@RoyiNamir:我自己写的。@NHAHDH:当然,是的。这个正则表达式适合提取,但不适合验证。我可能误解了Royi所说的“接受”的意思。@RoyiNamir:因为您在第二个备选方案的周围放错了括号,因此,\b
并没有应用于备选方案中的所有项目。试试这个:alert(/^(?=.*(\b(一月二月三月五月六月七月八月九月十月十一月十二月))(?=.*(\b[12]\d{3}\b))(?=.\b(0[1-9]|[12]\d[3])\b)/i.test('FEB cc 2012')代码>这仍然是一个常规(严格地说)表达式吗@蒂姆:这些文字评论是你自己写的,还是solftware写的?哪一个?@RoyiNamir:我自己写的。@NHAHDH:当然,是的。这个正则表达式适合提取,但不适合验证。我可能误解了Royi所说的“接受”的意思。@RoyiNamir:因为您在第二个备选方案的周围放错了括号,因此,\b
并没有应用于备选方案中的所有项目。试试这个:alert(/^(?=.*(\b(一月二月三月五月六月七月八月九月十月十一月十二月))(?=.*(\b[12]\d{3}\b))(?=.\b(0[1-9]|[12]\d[3])\b)/i.test('FEB cc 2012')代码>