I have the following regular expression
/^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i
To validate emails, I must also validate that the email does not contain the word "does not have" and the expression is /^((?!notiene).)*$
, however I cannot mix the two.
The problem . The negative lookahead you are using starts from the beginning of the text, and consumes each one of the characters as long as they are not followed by
notiene
This means that when you try to validate the mail, it will already be found at the end of the text. That is, the
.*
one of that pattern has already consumed all the characters, without allowing more to be validated.The solution . On the other hand, if it is validated from the beginning of the text, but no character is consumed, then more than one subpattern can be validated:
An inspection attempts to match the subpattern, and after matching, the cursor continues from the same position it was in before the inspection was attempted. It can be thought of as a construct that only returns true/false, but the rest of the regex can continue as if nothing had happened ( if it returned true, of course ).
With
^(?!.*notiene)
we make sure that the entire line is traversed. If.*notiene
it doesn't match, being a negative check then it's like it returns " true ", but we're still at the beginning of the text (no characters were consumed), and we can check any other patterns from there.Full Regex: (using the answer from Validate an email in JavaScript )
demonstration:
overall case
For any case where we have 2 regular expressions anchored at the beginning of the text (yes, they have to match from the beginning), let them be
^re1
and^re2
, the way to check both is:Or, as in your case, that the first does not match, but the second does:
For example, joining 3 expressions: that has a capital letter, that does not end in "1234" and that has between 8 and 200 characters .