【问题标题】:Regular expression to not accept . at the end of the email address in React不接受的正则表达式。在 React 中电子邮件地址的末尾
【发布时间】:2022-01-09 01:38:23
【问题描述】:

我想构造一个正则表达式,其中. 不允许作为电子邮件地址的最后一个和第一个字符(例如:.abc.@gmail.com 不应被接受)。另外,我有 另一个要求是.. 不应在电子邮件地址的任何位置允许连续两个句点(例如:abc...def@gmail.com)。

我目前使用的正则表达式是: let regEx = new RegExp(/^[a-zA-Z0-9+_.-]+@(\[(\d{1,3}\.)(([a-zA-Z\d-]+\.)+))([a-zA-Z]{2,15}|\d{1,3})(\]?)/);

我可以在修改上述正则表达式以满足我的要求方面获得一些帮助吗?

PS:我对正则表达式知之甚少。

【问题讨论】:

  • "abc...def"@gmail.com 将是一个有效的邮件地址
  • 是的,但出于我的要求,我不想要这个特定的。

标签: javascript reactjs regex


【解决方案1】:

添加您的 2 个新要求,我们可以尝试:

^(?!.*\.\.)[\p{L}\p{N}\s,_-][\p{L}\p{N}\s,._-]*[\p{L}\p{N}\s,_-]$

解释:

^                        from the start of the email
    (?!.*\.\.)           no two or more consecutive dots
    [\p{L}\p{N}\s,_-]    first character is not dot
    [\p{L}\p{N}\s,._-]*  zero or more allowed characters, including dot
    [\p{L}\p{N}\s,_-]    last character is not dot
$                        end of the email

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-08
    • 2015-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多