开发者

new RegExp. test

开发者 https://www.devze.com 2023-01-18 23:50 出处:网络
I have posted a problem in the above link - regExpression.test. Based on that I have done like bellow that also produces an error.

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

Based on that I have done like bellow that also produces an error.

var regExpression=new RegExp("^([a-zA-Z0-9_\-开发者_如何学运维\.]+)$");
alert (regExpression.test("11aa"));


You need to escape your \ since you're declaring it with a string, like this:

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

You can test it here.


You can also use the literal RegExp syntax /…/:

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

By the way: The . does not need to be escaped in character classes anyway. And if you put the range operator at the begin or the end of the character class or immediately after a character range, it doesn’t need to be escaped either:

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

精彩评论

暂无评论...
验证码 换一张
取 消