简体   繁体   中英

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 . I tried above code. but it success for 03/2014/02.I want only mm//yyyy format

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".

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年。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM