2 regular expressions in 1

Asked

Viewed 183 times

4

I found the following regular expression to validate javascript email address:

/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/

Which works really well. I would like to add, in the same expression, the minimum and maximum character limitations:

/^.{3,100}$/

A less elegant solution is to do 2 checks (which is what I’m doing today). It is possible to leave everything in a single expression?

  • 2

    I don’t know if you noticed, but your regex already measures the size of specific fragments of your expression. You can see, by the way, that there are several {1,3} there in the middle that make all the difference. You can know more by testing your expressions and rules using an online Bugger, such as the Regexpal.

  • 2

    It is necessary to be very careful with the validation of email, in my opinion it is convenient to make it as simple as possible, since there is a huge variety of possible emails. I do a very simple validation to see if there is: [email protected], that’s all.

  • 1

    @Jorgeb. Although I agree in principle that making it simple is good, one must be careful not to have false negatives. For example, I’ve been using the formula [email protected] to prevent spam (and to find out which website I registered for sold my email to spammers), but many sites rejected it because of + at the address - which is something perfectly valid.

  • 1

    @mgibsonbr when I say texto@ I meant any valid character.

  • Advice: Use the Jquery validate for the validation.

  • @mgibsonbr Are they "false positives"? Haha.

  • 1

    I wouldn’t recommend that. Unless you have thought about ALL possible users' over-stupidity and are protected against ALL the possibilities of this ER failing (which is extremely difficult in most scenarios), sooner or later this ER will need maintenance, if you support yourself or someone else. And let’s come and agree that reading a complex ER is not so easy.

  • 1

    @Brunoaugusto If the logic of the OP depends on capture groups within this regex, then I agree with your argument. Otherwise, would it not simply be the case to replace this regex with another case if it needs maintenance? Validating emails is a common task, and there are initiatives to find good regexes that are actually compatible with the specification (example). It is a complex task, and ad-hoc solutions have great chance to contain IMHO errors.

  • Yes, but a very large ER makes it difficult to even rewrite a new revision of it because invariably the programmer will have to read and interpret it. I don’t know if JS has ER support multiline like PHP where you can comment on each segment of it.

  • Mercy, what a catastrophe was the wording of my penultimate comment.

  • The answer is correct but I agree with the comments and benefits of a simple validation since it is a standard that I do not have full control and can change regardless of my business rules.

Show 6 more comments

1 answer

5


Yes, it is possible using lookarounds:

(?=regex1)regex2
(?!regex1)regex2

In the first case (Positive Lookahead), the engine checks if the string matches the regex1, but without actually consuming it, and then proceeds to the normal marriage with the regex2. In the second (Negative Lookahead) is the same thing, but the engine checks if the string nay house with regex1.

Adapting to your case would look like this:

(?=^.{3,100}$)^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$

Example in jsFiddle (I used 15 to 20 characters to simplify the test). Source: that question on Soen.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.