Kuttan Sujith
Kuttan Sujith

Reputation: 7979

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"));

Upvotes: 1

Views: 3269

Answers (2)

Gumbo
Gumbo

Reputation: 655269

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_.-]+)$/;

Upvotes: 3

Nick Craver
Nick Craver

Reputation: 630429

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.

Upvotes: 3

Related Questions