我想为所有包括非斜体字符的电子邮件地址编写正则表达式。
我试过了,但它返回错误
请尽快提供正确的解决方案
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/xregexp/3.1.1/xregexp-all.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script type="text/javascript">
var em = XRegExp('^([\\p{L}+|\\p{N}*][@][\\p{L}+][.][\\p{L}+])$'); // Please help me to correct it
jQuery(function(){
jQuery('input').blur(function(){
console.log(jQuery(this).val());
console.log(em.test(jQuery('#t1').val()));
});
});
</script>
<title></title>
</head>
<body>
Enter Name: <input type="text" name="t1" id="t1" class="kcd">
</body>
</html>
最佳答案
虽然有更好的方法来确保您的电子邮件正则表达式有效(请参阅 @Tushar 的 comment ),但我想解释一下您的正则表达式的问题所在。
^([\\p{L}+|\\p{N}*][@][\\p{L}+][.][\\p{L}+] )$
包含格式不正确的字符类 [\\p{L}+|\\p{N}*]
和 [\\p{L}+]
。它们匹配其中定义的单个字符 - [\\p{L}+|\\p{N}*]
匹配 p
、{
、L
等和 [\\p{L}+]
匹配 p
、{
、L
、 或
+
。
如果您打算使用您的方法,您可能希望将正则表达式修复为
XRegExp('^[\\p{L}\\p{N}]+@\\p{L}+[.]\\p{L}+$')
详细信息:
^
- 字符串的开始[\\p{L}\\p{N}]+
- 一个或多个 Unicode 字母或数字@
- “at”符号\\p{L}+
- 一个或多个 Unicode 字母[.]
- 文字点\\p{L}+
- 同上。$
- 字符串结尾。
关于javascript - 电子邮件地址的 XRegExp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39286442/