Alex Rivera | Logout

RegExp.test not working?

Asked 2011-07-22T17:25:37.603
17

I am trying to validate year using Regex.test in javascript, but no able to figure out why its returning false.

var regEx = new RegExp("^(19|20)[\d]{2,2}$"); 

regEx.test(inputValue) returns false for input value 1981, 2007

Thanks

Edit
Report

2 Answers

12

Found the REAL issue:

Change your declaration to remove quotes:

var regEx = new RegExp(/^(19|20)[\d]{2,2}$/); 
answered 2011-07-22T17:28:14.957
1

I've noticed, for reasons I can't explain, sometimes you have to have two \\ in front of the d.

so try [\\d] and see if that helps.

answered 2011-07-22T17:30:45.573

Your Answer