new RegExp. test
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_.-]+)$/;
精彩评论