71

I don't know why my regex is incorrect:

var domain = "google\.com\.br";
var reEmail = new RegExp("^([A-Za-z0-9_\-\.])+\@" + domain + "$");

I need this to validate an email. Example below: reEmail.test("[email protected]");

I get this error:

Range out of order in character class

1
  • 8
    Use a regex literal: /^([A-Za-z0-9_\-.])+@/!!! Commented Jul 18, 2013 at 15:49

2 Answers 2

143

Because you create the RegExp using a String the _\-\. becomes _-. and that is the invalid range. (It is a range from _ to . and that is not correct)

You need to double escape it:

new RegExp("^([A-Za-z0-9_\\-\\.])+@" + domain + "$");

That way the \\ becomes a \ in the String and then is used to escape the -in the RegExp.

EDIT:

If you create RegExp by String it is always helpful to log the result so that you see if you did everything right:

e.g. your part of the RegExp

console.log("^([A-Za-z0-9_\-\.])+\@");

results in:

^([A-Za-z0-9_-.])+@
Sign up to request clarification or add additional context in comments.

5 Comments

On a side note, a quick way to handle this is, if you need to allow a - as a valid character in your character group, always place it as either the first or last character in the group (e.g., [-a-zA-Z0-9 _] or [a-zA-Z0-9 _-]). Regex is smart enough to know that you are not specifying a range with that character, if it is in those positions (without needing to escape it with a slash).
@talemyn Sure thats true. Personally i prefer to escape it (and also set this into our coding guideline) because it has a special meaning.
This should be accepted answer. nice addition by @talemyn also
@talemyn That's a very cool trick. However, for any project that involves more than one developer and one day, it is possible that the next developer (including yourself tomorrow) might not know/remember this trick! So, when they want to add a new character (say #) to the range they might just put it at the beginning (i.e. [#-a-zA-Z0-9_]) and it will take the whole company three days to figure out why it's now broken!
@Aidin - Going to have to disagree with you there. It's not a trick, it's simply one of the potential behaviors of dashes in the JavaScript flavor of regex . . . really no different than understanding how a slash behaves when you use it like this: "\" vs this: "\\". Now I'm not saying that it's one of the better known behaviors, but just because a lot of people don't know about a feature doesn't mean that you shouldn't use it . . . that's how more people eventually learn about it.
0

this is because RegEx works with STRINGS not numbers, so for 2 digit number ranges like: 1 to 16, you CAN NOT use /[1-16]/ = ERROR NOT A VALID RANGE

You need to do this:

/([1-9]|1[0-6])/;

NOTE: in the above you also need to include VALUE < 17, otherwise regex will match anything that has 6 in it, like 26, 36 etc., as well as other number that you don't want.

You can also go this route:

/1|2|3...|15|16/    

in the above you need to replace ... with |4|5.. etc.

See here for REGEX for Ranges: https://3widgets.com/

alternatively, try to use FOR LOOP to filter or test stuff out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.