RegExp.test returns false, even though it should return true
11 Aug 2022typescript
For example, this is my regular expression :
^\d{4}\/(0?[1-9]|1[012])\/(0?[1-9]|[12][0-9]|3[01])$
It used to check if string is in YYYY-MM-DD form.
I used code as below at first, and found that regexp.test
always return false, even thouth it should return true.
const regexp = new RegExp('^\d{4}\/(0?[1-9]|1[012])\/(0?[1-9]|[12][0-9]|3[01])$')
console.log(regexp.test(date))
The solution is :
const regexp = new RegExp(/^\d{4}\/(0?[1-9]|1[012])\/(0?[1-9]|[12][0-9]|3[01])$/)
return(regexp.test(date))
To replace single quotations(‘) to slash(/) ! Check this website to get more information.