admin管理员组文章数量:1296308
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 es down to 1 question. How can I correct the JavaScript regex to match that of the Python regex?
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 es down to 1 question. How can I correct the JavaScript regex to match that of the Python regex?
Share edited May 21, 2020 at 23:28 Max asked Oct 25, 2016 at 4:11 MaxMax 2,1625 gold badges29 silver badges46 bronze badges 6-
1
[^@]
=> Negated character class. Not@
– Tushar Commented Oct 25, 2016 at 4:12 - 1 No need to change anything in regex, use it as it is. And, maybe add anchors. – Tushar Commented Oct 25, 2016 at 4:14
- Hi @Tushar the python regex is correct, it is the javascript I'm worried about. – Max Commented Oct 25, 2016 at 4:16
-
2
The python regex can be used as is in JavaScript:
/[^@]+@[^@]+\.[^@]+/
– castletheperson 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. – Max Commented Oct 25, 2016 at 4:27
1 Answer
Reset to default 10Simply convert python regex to Javascript.
Python: r"[^@]+@[^@]+\.[^@]+"
Javascript: /[^@]+@[^@]+\.[^@]+/
本文标签: Regex email validation Python to JavascriptStack Overflow
版权声明:本文标题:Regex email validation Python to Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741601175a2387709.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论