Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regexp .test always returns false

var str = "1405199610";
var re = new RegExp("\d{10}");
var myArray = re.test(str);

myArray gives false!

How is this possible? I tested this with online regex checkers and it checks out ok. When i try it in the console or jsfiddler, it doesn't work. Am I missing something?

like image 758
Paris Char Avatar asked Dec 12 '22 01:12

Paris Char


1 Answers

var re = new RegExp("\\d{10}");

you need to escape the \ when regexp is created by RegExp() object

See MDN reference

...Also do not forget to escape \ itself while using the new RegExp("pattern") notation since \ is also an escape character in strings.

like image 90
Fabrizio Calderan Avatar answered Dec 21 '22 14:12

Fabrizio Calderan