New RegExp. test

I posted the problem in the link above - regExpression.test .

Based on what I did as shown below, it also throws an error.

var regExpression=new RegExp("^([a-zA-Z0-9_\-\.]+)$");
alert (regExpression.test("11aa"));
+1
source share
2 answers

You need to escape with \since you declare it as a string, for example:

var regExpression=new RegExp("^([a-zA-Z0-9_\\-\\.]+)$"); 
                                           ^  ^ add these

You can test it here .

+3
source

You can also use RegExp literal syntax/…/ :

var regExpression = /^([a-zA-Z0-9_\-\.]+)$/;

: . . , :

var regExpression = /^([a-zA-Z0-9_.-]+)$/;
+3

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


All Articles