I'm trying to validate only 4 numbers like this:
It is working on the above page, but when I use it on a script it is not working:
var reg = new RegExp('^\d{4}$/');
reg.test(1234);
reg.test('1234');
Both are returning false...
If I test on the browser console like this:
/^\d{4}$/.test('1234');
/^\d{4}$/.test(1234);
Both are returning true.
What I'm missing?
/^\d{4}$/.test(1234)directly in your code ?var reg = RegExp(/^\d{4}$/)'^\d{4}$/'should be'/^\d{4}$/'RegExpconstructor. Also the\dneeds to have its slash escaped in aRegExpstring parameter (i.e.,\\d). Basically, Chin Leung nailed it in his answer below. :)