I’ve just inherited a projected with a lot of client side validation.
In this I have found a regex checker, as expected without a comment in sight. I’ll admit regex is definitely one of my failing points. (In Javascript!)
var myRegxp = /(([0]*[1-9]{1})|([1]{1}[0-2]{1}))(\/{1})((19[0-9]{2})|([2-9]{1}[0-9]{3}))$/;
if (!myRegxp.test(args.Value))
{//do fail stuff here};
I’m pretty sure from the rest of the page that it’s supposed to be checking for some sort of date format. Would this pass MM/YYYY. From some early testing, it almost does it.
If this regex doesn’t match MM/YYYY, what would be the best way to do this?
Any advice from the regex masters would be appreciated.
It should end with MM/YYYY (where MM is 1-12 and YYYY is 1900-9999)
(matching month)
OR
Second part (year), first it literally match ‘/’ with:
Then the year:
OR
A simplified RE:
Note:
\dis a character class for0-9. I’ve removed parentheses used for grouping because these are not used inmyRegExp.test.ReplacedReplaced[0]*by0?since it should not match0000001/2010, but it should match1/2010.[0]*by0since it should literally match01and not1. The^and$are anchors, causing it to match from begin to end. When leaving these out, this RE would match any text containing MM/YYYY.