javascript - 根据提到的要点在javascript中发送电子邮件正则表达式

标签 javascript jquery regex email input

谁能提供以下条件的正则表达式

  1. 用户部分不以“.”开头或结尾性格。
  2. 域部分不包含以下无效字符:"#\"%|<>?'`/ ,*&;:£$^!~ ú(){}+"
  3. 有效的电子邮件包含“@​​”和“.”字符
  4. 有效的电子邮件不包含两个连续的点

目前我正在使用下面的 reg ex

 /^(?!^[.])[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]{2,63})*$/;

提到的正则表达式并非在所有方面都有效。

最佳答案

为了尊重所有这些条件,您需要按顺序处理它们并采用逻辑来编写您的正则表达式,我尝试为您的情况制定合适的解决方案,这就是 the Regex 我想出了:

(?!^[.])[a-zA-Z_\-0-9]+[a-zA-Z_\-0-9.]+(?!\.\.)[^\.]+@(?![#\"%|<>?'`\/ ,*&;:£$^!~ ú(){}+])(?![\.]{2})[a-zA-Z_\-0-9.]+\.[a-zA-Z]{2,3}

演示:

您可以 test this Regex here并在 a working live Demo 中查看也是。

说明:

  • 负前瞻 (?!^[.])

Assert that the Regex below does not match ^ asserts position at start of the string Match a single character present in the list below [.] . matches the character . literally (case sensitive)

  • 匹配下面列表中的单个字符 [a-zA-Z_\-0-9]+

I added this part to avoid matching the . in the beginning of the regex + Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy) a-z a single character in the range between a and z (case sensitive), A-Z a single character in the range between A and Z _ matches the character _ literally (case sensitive) - matches the character - literally (case sensitive) 0-9 a single character in the range between 0 and 9

  • 匹配下面列表中的单个字符 [a-zA-Z_\-0-9.]+

+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy) a-z a single character in the range between a and z (case sensitive), A-Z a single character in the range between A and Z _ matches the character _ literally (case sensitive) - matches the character - literally (case sensitive) 0-9 a single character in the range between 0 and 9, . matches the character . literally (case sensitive)

  • 负前瞻 (?!\.\.)

Assert that the Regex below does not match \. matches the character . literally (case sensitive) \. matches the character . literally

  • 匹配一个不在下面列表中的字符 [^\.]+

+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy) \. matches the character . literally

  • @匹配字符 @从字面上看
  • 负前瞻 (?![#\"%|<>?'\/ ,*&;:£$^!~ ú(){}+])

Assert that the Regex below does not match Match a single character present in the list below [#\"%|<>?'/ ,*&;:£$^!~ ú(){}+]`

  • 负前瞻 (?![\.]{2})

Assert that the Regex below does not match Match a single character present in the list below [\.]{2}, {2} Quantifier — Matches exactly 2 times \. matches the character . literally

  • 匹配下面列表中的单个字符 [a-zA-Z_\-0-9.]+

+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed a-z a single character in the range between a and z (case sensitive), A-Z a single character in the range between A and Z (case sensitive) _ matches the character _ literally (case sensitive) - matches the character - literally (case sensitive) 0-9 a single character in the range between 0 and 9, . matches the character . literally (case sensitive) \. matches the character . literally

  • 匹配下面列表中的单个字符 [a-zA-Z]{2,3}

{2,3} Quantifier — Matches between 2 and 3 times, as many times as possible, giving back as needed a-z a single character in the range between a and z (case sensitive), A-Z a single character in the range between A and Z

关于javascript - 根据提到的要点在javascript中发送电子邮件正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46170091/

相关文章:

javascript - React.js 为什么组件在更改 props 后不会立即重新渲染

javascript - 最紧凑的 JSON 数据 URL 编码?

javascript - Angular ngRepeat - HTML 更改后更新

c# - 如何从格式化字符串中删除空行

JavaScript 正则表达式匹配大写链接

JavaScript - 尝试访问数组但未定义

javascript - jQuery - 如何在动画中进行数学运算

javascript - 是否可以对 JavaScript 而不是 ASP 进行 AJAX 调用?

jquery - 使用 jQuery 至少选择表单中的一个复选框

regex - PCRE:回溯中不允许反向引用?