The eval seems to not understand the RegEx at all.
I got this string to Check for entered YEAR in the input field.
The RegEx should match something like: 2010, 1980 or any Year between 1900 to 2099.
RegEx: ^(19|20)\d{2}$ works well in raw javascript something like:
var testStr = /^(19|20)\d{2}$/.test(2013) //logs true
but testStr2 = eval("/^(19|20)\d{2}$/").test(2013) //logs false
Please see fiddle: http://jsfiddle.net/zq30puzm/
This is being used in:
"getYear":{
"regex":"/^(19|20)\d{2}$/",
"alertText":"* Invalid Year"
},
It keeps on printing the Error event when the input data is a deserving one.
What could be the issue?
Any suggestion is appreciated.
Fiddle: http://jsfiddle.net/zq30puzm/
evalis evil.eval("/.../")is functionally equivalent tonew Regexp("..."), but much slower and less safe. Almost everything in JavaScript can be done withouteval- and should be.new RegExp? I tried;var str=new RegExp("/^(19|20)\d{2}$/"); str.test(2013) //logs false.How best to do it usingnew RegExp()??new Regexpyou're using a string literal as a parameter, and in a string literal backslashes need to be escaped.