I've created a regex to filter some email domains from my form:
/\@(?!(hotmail|msn)\.)/
This filters emails with domains of hotmail and msn successfully.
Now, I also want to optionally filter any emails that end with a digit. I created optional groupings. Group 1 checks for a digit at the end of the string before '@'. Group 2 checks for the domain.
Example of emails that are not allowed:
Example of emails that are allowed:
/(?:(?!(\d \b)))?\@(?:(?!(hotmail|msn)\.))/
This filters emails with domains of hotmail and msn successfully, but does not filter an example like [email protected]
What is the issue?
CodePudding user response:
You could write the pattern as:
[^@\s\d] @(?!(?:hotmail|msn)\.)[^@\s]
Explanation
[^@\s\d]
Match 1 chars other than a whitespace char@
Match literally(?!(?:hotmail|msn)\.)
Negative lookahead, assert not hotmail. or msn. directly to the right of the current position[^@\s]
Match 1 chars other than @ or a whitespace char