5

I'm having trouble fully understanding this Python 2 regular expression to validate email addresses. I found a great example here.

r"[^@]+@[^@]+\.[^@]+"

So according to regex101 this means we need atleast 1 value before the '@' symbol, 1 '@' symbol, and any number of characters after the '@' symbol and atleast 1 '.' symbol. First of all correct me if I'm wrong, but secondly no documentation anywhere explains what "[^@]" means. So I'm having trouble understanding what the above code means.

All I would like to do is convert the above code to Javascript but have only found help with the following case:

/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/

This means very little to me, but have found this useful example here. The problem is that the JavaScript expression over checks.

For example, the following string passes in Python but fails the JS expression:

[email protected]/org

It really comes down to 1 question. How can I correct the JavaScript regex to match that of the Python regex?

6
  • 1
    [^@] => Negated character class. Not @ Commented Oct 25, 2016 at 4:12
  • 1
    No need to change anything in regex, use it as it is. And, maybe add anchors. Commented Oct 25, 2016 at 4:14
  • Hi @Tushar the python regex is correct, it is the javascript I'm worried about. Commented Oct 25, 2016 at 4:16
  • 2
    The python regex can be used as is in JavaScript: /[^@]+@[^@]+\.[^@]+/ Commented Oct 25, 2016 at 4:20
  • wow. stupid me, I was unaware that the regex transferred so well. @4castle please submit your solution so I can mark as correct. Commented Oct 25, 2016 at 4:27

1 Answer 1

10

Simply convert python regex to Javascript.

Python: r"[^@]+@[^@]+\.[^@]+"

Javascript: /[^@]+@[^@]+\.[^@]+/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.