正则表达式专门组合月份名称和号码

时间:2011-07-04 12:15:29

标签: regex

我正在尝试使用一个正则表达式验证月份编号或月份名称。

我有

/^
0?([1-9]|1[012])                  ---number check 0?1-12
|\b(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May?|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Sep(?:t)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)$/        ---name check

这两个检查分别工作正常,我认为我没有正确加入表达,任何帮助赞赏。

1 个答案:

答案 0 :(得分:4)

观察您进行更改的位置:

/^
(?:                 # otherwise the ^ becomes part of the first alternation
 (0?[1-9]|1[012])   # number check 0?1-12
|
 (?:                # name check
  Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|
  Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sept?|September|
  Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?
 )
)
$/