[英]Regular Expression for MM/YYYY in Javascript
var filter = new RegExp("(0[123456789]|10|11|12)([/])([1-2][0-9][0-9][0-9])");
I want to check given text must be month and year only(mm/yyyy) like 03/2014 . 我想检查给定的文本必须仅是月份和年份(mm / yyyy),如03/2014。 I tried above code.
我尝试了上面的代码。 but it success for 03/2014/02.I want only mm//yyyy format
但它在03/2014/02上成功。我只希望mm // yyyy格式
Please help me, I want only month and year in that format. 请帮助我,我只希望月份和年份采用这种格式。
Your code is nearly fine. 您的代码几乎可以了。 The only thing you are missing are anchors .
您唯一缺少的是锚点 。 They are important to avoid partial matches, like you see when it (partly) matches on "03/2014/02".
它们对于避免部分匹配非常重要,就像您看到“ 03/2014/02”上的部分匹配时一样。
So, add the anchors ^
(start of the string) and $
(end of the string) to your regex. 因此,将锚
^
(字符串的开头)和$
(字符串的结尾)添加到您的正则表达式中。
var filter = new RegExp("^(0[123456789]|10|11|12)([/])([1-2][0-9][0-9][0-9])$");
^(0[1-9]|1[0-2])/(19|2[0-1])\d{2}$
尝试此操作可匹配月份(1-12)和1900-2199年。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.