Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx conversion to use with Javascript

I'm currently using this RegEx ^(0[1-9]|1[0-2])/(19|2[0-1])\d{2}$ in .NET to validate a field with Month and Year (12/2000).

I'm changing all my RegEx validations to JavaScript and I'm facing an issue with this one because of /in the middle which I'm having problems escaping.

So based on other answers in SO I tried:

    RegExp.quote = function (str) {
        return (str + '').replace(/[.?*+^$[\]\\(){}|-]/g, "\\$&");
    };
    var reDOB = '^(0[1-9]|1[0-2])/(19|2[0-1])\d{2}$'

            var re = new RegExp(RegExp.quote(reDOB));
            if (!re.test(args.Value)) {
                args.IsValid = false;
                return;
            }

However, validations fails even with valid data.

like image 939
Diomedes Avatar asked Nov 10 '22 11:11

Diomedes


1 Answers

Remove ^ of first and $ from end of regex pattern. And add \ before any character which you want to match by pattern. so pattern is like this:

(0[1-9]|1[0-2])\/(19|2[0-1])\d{2}

You can test your regex from here

like image 171
Behzad Avatar answered Nov 15 '22 12:11

Behzad