1

My coworker and I were trying to match test emails from a given domain for automatic whitelisting.
We are trying to match emails of the form "any.random.nonsense+test23@nothing.com"

Our original RegEx was /\+test[0-9]+@nothing.com/g. This matches only emails containing even digits. Very strange and unexpected.

We thought that maybe it was a problem with an interaction between the @ symbol and the preceding digits, so we escaped the @. We then tried /\+test[0-9]+\@nothing.com/g but that only matched emails of the given form with odd digits. Also very strange.

We were able to get the regex working by leaving off the global tag at the end /\+test[0-9]+@nothing.com/. This matches everything, but we're not sure why.

Original RegEx test:

1

Escaped @ symbol RegEx test:

2

(I would post a third link to the working version, but I don't have enough reputation)

Can anybody help explain why we see this behaviour? What is going on here?

  • Is this what you are looking for? https://www.regex101.com/r/2E72IU/1 – online Thomas Mar 07 '17 at 21:00
  • I hope you were able to find your answer. In the future, please share the code (paste code, select all and press **`{ }`** in the wysiwyg editor) and not images of the code. Otherwise, well-built question. You described your efforts, your problem, and showed what should and should not pass. This is so people can easily copy and paste your samples to sites like http://www.regex101.com – Regular Jo Mar 07 '17 at 22:20
  • Thank you for the pointers @RegularJoe I'm getting there and look forward to being part of the StackExchange community. I did find my answer. The global-tag was setting the lastIndex property of the regEx, which actually made it compare the string from the last index in the string forward every other time it was ran, effectively matching it on a null string. Indeed it was not even and odd numbers that it failed on, but every other pass. Thank you for your help. – CollectionOfAtoms Mar 08 '17 at 06:13

0 Answers0