谁能提供以下条件的正则表达式
- 用户部分不以“.”开头或结尾性格。
- 域部分不包含以下无效字符:"#\"%|<>?'`/ ,*&;:£$^!~ ú(){}+"
- 有效的电子邮件包含“@”和“.”字符
- 有效的电子邮件不包含两个连续的点
目前我正在使用下面的 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 betweena
andz
(case sensitive),A-Z
a single character in the range betweenA
andZ
_
matches the character _ literally (case sensitive) - matches the character - literally (case sensitive)0-9
a single character in the range between0
and9
- 匹配下面列表中的单个字符
[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 betweena
andz
(case sensitive),A-Z
a single character in the range betweenA
andZ
_
matches the character _ literally (case sensitive) - matches the character - literally (case sensitive)0-9
a single character in the range between0
and9
,.
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 neededa-z
a single character in the range betweena
andz
(case sensitive),A-Z
a single character in the range betweenA
andZ
(case sensitive)_
matches the character _ literally (case sensitive) - matches the character - literally (case sensitive)0-9
a single character in the range between0
and9
,.
matches the character.
literally (case sensitive)\.
matches the character.
literally
- 匹配下面列表中的单个字符
[a-zA-Z]{2,3}
{2,3}
Quantifier — Matches between2
and3
times, as many times as possible, giving back as neededa-z
a single character in the range betweena
andz
(case sensitive),A-Z
a single character in the range betweenA
andZ
关于javascript - 根据提到的要点在javascript中发送电子邮件正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46170091/