2

我想为所有包括非斜体字符的电子邮件地址编写正则表达式。

我试过但它返回错误

请尽快提供正确的解决方案

 <!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>

4

1 回答 1

1

虽然有更好的方法来确保您的电子邮件正则表达式有效(请参阅@Tushar评论),但我想解释一下您的正则表达式存在什么问题。

包含格式不正确的^([\\p{L}+|\\p{N}*][@][\\p{L}+][.][\\p{L}+])$字符类[\\p{L}+|\\p{N}*][\\p{L}+]. 它们匹配其中定义的单个字符 -[\\p{L}+|\\p{N}*]匹配 a p{L等,并[\\p{L}+]匹配 a p{L}+

如果您打算使用您的方法,您可能希望将正则表达式修复为

XRegExp('^[\\p{L}\\p{N}]+@\\p{L}+[.]\\p{L}+$')

详情

  • ^- 字符串的开始
  • [\\p{L}\\p{N}]+ - 一个或多个 Unicode 字母或数字
  • @- “在”符号
  • \\p{L}+- 一个或多个 Unicode 字母
  • [.]- 一个字面点
  • \\p{L}+- 同上。
  • $- 字符串结束。
于 2016-09-02T07:25:24.073 回答