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 rigs and it checks everything is fine. When I try to use it in the console or jsfiddler, it does not work. Did I miss something?

+4
source share
1 answer
 var re = new RegExp("\\d{10}"); 

you need to avoid \ when regexp creates a RegExp() object

See MDN Link

... Also, don't forget to escape \ when using the new RegExp ("pattern") entry, as \ is also an escape character in strings.

+12
source

Source: https://habr.com/ru/post/1414163/


All Articles