Is this usage of hyphen in regex valid?

NB. I only want to know if it's a valid application of unescaped hyphen in the regex definition. It's not a question about matching email, meaning of hyphen nor backslash, quantifiers or anything else. Also, please note that the linked in answer doesn't really discuss the validity issue between escaped/unescaped hyphen.

Usually I declare the regex for matching email addresses like this.

var emailPattern = /^[a-z.\-_][email protected][a-z]+[.]{1}[a-z]{2,3}$/;
emailPattern.test('[email protected]');

Now, by mistake, a colleague of mine forgot the escape character and **still* made it work, which surprised me, because of the interval meaning of the hyphen. It looks like this.

var weirdPattern = /^[a-z._-][email protected][a-z]+[.]{1}[a-z]{2,3}$/;
weirdPattern.test('[email protected]');

Apparently, it works because the hyphen is the last character in the brackets. My question is if this is just a happy coincidence or if it's a valid syntax? Have I been regexing wrong my whole life?

Answers:

Answer

Hyphens inside character class are used for range. However, when put at the beginning or at the end inside character class there is no need of escaping that.

Note that, in some browsers, hyphens at any position in the character class are still considered as range metacharacters, so it is best practice to always escape it.

Quoting from regular-expressions.info

The hyphen can be included right after the opening bracket, or right before the closing bracket, or right after the negating caret. Both [-x] and [x-] match an x or a hyphen. [^-x] and [^x-] match any character that is not an x or a hyphen. Hyphens at other positions in character classes where they can't form a range may be interpreted as literals or as errors. Regex flavors are quite inconsistent about this.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.