[英]javascript regular expression - how to allow only one hyphen?
I have the following jscript code: 我有以下jscript代码:
var email = /^[a-z0-9'*+/=?^_`{|}~-]+(?:\.[a-z0-9'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
if (s.match(email)) {
return true;
} else {
return false;
}
Now the end users are saying that they want "-" in the local part of the email address. 现在,最终用户说他们希望在电子邮件地址的本地部分显示“-”。 (i didn't even know that was legit) But I need to prevent multiple hyphens I think - as per http://rumkin.com/software/email/rules.php And also I don't think it can start with a hyphen.
(我什至不知道这是合法的),但我需要防止多个连字符出现-根据http://rumkin.com/software/email/rules.php,而且我也不认为它可以以连字符。
But i'm not sure how to change this code to accomplish that. 但是我不确定如何更改此代码来实现这一目标。
What I've tried 我尝试过的
var email = /^[a-z0-9'*+/=?^_`{|}~-]+(?:\.\-[a-z0-9'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
That allows me to enter a hyphen, but it doesn't limit it to one. 这使我可以输入连字符,但不限于此。
You are not restricted to do everything in one regex: 您不受限于在一个正则表达式中做所有事情:
var email = /^[a-z0-9'*+\/=?^_`{|}~-]+(?:\.[a-z0-9'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/;
if (!s.match(email)) return false;
if (s[0] == '-') return false;
if (s[s.length-1] == '-') return false;
if (s.indexOf('--') != -1) return false;
return true;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.