Skip to content Skip to sidebar Skip to footer

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_\-\.]+

Solution 1:

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.

Solution 2:

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

Post a Comment for "New Regexp. Test"