用于确定电子邮件域的 JavaScript RegEx(例如 yahoo.com)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3270185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript RegEx to determine the email's domain (yahoo.com for example)
提问by AnApprentice
With JavaScript I want to take a input 1st validate that the email is valid (I solved for this) 2nd, validate that the email address came from yahoo.com
使用 JavaScript 我想输入第一个验证电子邮件是否有效(我解决了这个问题)第二个,验证电子邮件地址来自 yahoo.com
Anyone know of a Regex that will deliver the domain?
任何人都知道将提供域的正则表达式?
thxs
谢谢
回答by Tim Pietzcker
var myemail = '[email protected]'
if (/@yahoo.com\s*$/.test(myemail)) {
console.log("it ends in @yahoo");
}
is true if the string ends in @yahoo.com(plus optional whitespace).
如果字符串以@yahoo.com(加上可选的空格)结尾,则为真。
回答by styfle
You do not need to use regex for this.
您不需要为此使用正则表达式。
You can see if a string contains another string using the indexOfmethod.
您可以使用该indexOf方法查看一个字符串是否包含另一个字符串。
var idx = emailAddress.indexOf('@yahoo.com');
if (idx > -1) {
// true if the address contains yahoo.com
}
We can take advantage of slice()to implement "ends with" like so:
我们可以利用slice()来实现“以”结束,如下所示:
var idx = emailAddress.lastIndexOf('@');
if (idx > -1 && emailAddress.slice(idx + 1) === 'yahoo.com') {
// true if the address ends with yahoo.com
}
In evergreen browsers, you can use the built in String.prototype.endsWith()like so:
在常绿浏览器中,您可以像这样使用内置的String.prototype.endsWith():
if (emailAddress.endsWith('@yahoo.com')) {
// true if the address ends with yahoo.com
}
See the MDN docsfor browser support.
有关浏览器支持,请参阅MDN 文档。
回答by V? Nh?t Anh
function emailDomainCheck(email, domain)
{
var parts = email.split('@');
if (parts.length === 2) {
if (parts[1] === domain) {
return true;
}
}
return false;
}
:)
:)
回答by casablanca
To check for a particular domain (yahoo.com):
要检查特定域 (yahoo.com):
/^[^@\s][email protected]$/i.test(email)
// returns true if it matches
To extract the domain part and check it later:
提取域部分并稍后检查:
x = email.match(/^[^@\s]+@([^@\s])+$/)
// x[0] contains the domain name
回答by Dorjee Karma
Try this:
试试这个:
/^\w+([\.-]?\w+)*@\yahoo.com/.test("[email protected]"); //Returns True
/^\w+([\.-]?\w+)*@\yahoo.com/.test("you@[email protected]"); //Returns false
/^\w+([\.-]?\w+)*@\yahoo.com/.test("you#[email protected]"); //Returns false
/^\w+([\.-]?\w+)*@\yahoo.com/.test("you/[email protected]"); //Returns false
Above are some test cases.
以上是一些测试用例。
回答by Gajender Singh
Domain name is mandatory like .com , .in , .uk It'll check update 2 letter after '.' and '@' is also mandatory.
域名是强制性的,如 .com , .in , .uk 它将检查更新 '.' 后的 2 个字母。'@' 也是强制性的。
<!DOCTYPE html>
<html>
<body>
<script>
function validateEmail(email) {
debugger;
console.log(email);
var re = /^(([^<>()\[\]\.,;:\s@"]+(\.[^<>()\[\]\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
//return ture for .com , .in , .co upto 2 letter after .
console.log(re.test(String(email).toLowerCase()));
return re.test(String(email).toLowerCase());
}
</script>
<h2>Text field</h2>
<p>The <strong>input type="text"</strong> defines a one-line text input field:</p>
<form action="#" onSubmit="validateEmail(firstname.value)">
First name:<br>
<input type="email" name="firstname">
<br>
Last name:<br>
<input type="text" name="lastname">
<br><br>
<input type="submit">
</form>
<p>Note that the form itself is not visible.</p>
<p>Also note that the default width of a text field is 20 characters.</p>
</body>
</html>
回答by Tobias Cohen
>>> String(?'[email protected]').replace????????(/^[^@]*@/, '')
'yahoo.com'
回答by serghei
For Yahoo domains (without username)
对于 Yahoo 域(没有用户名)
@(((qc|ca)?\.yahoo\.com)|yahoo\.(com(\.(ar|au|br|co|hr|hk|my|mx|ph|sg|tw|tr|vn))?|ae|at|ch|es|fr|be|co\.(in|id|il|jp|nz|za|th|uk)|cz|dk|fi|de|gr|hu|in|ie|it|nl|no|pl|pt|ro|ru|se))
回答by Leniel Maccaferri
What about this?
那这个呢?
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<script type="text/javascript">
var okd = ['yahoo.com'] // Valid domains...
var emailRE = /^[a-zA-Z0-9._+-]+@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})$/
function ckEmail(tst)
{
var aLst = emailRE.exec(tst)
if (!aLst) return 'A valid e-mail address is requred';
var sLst = aLst[1].toLowerCase()
for (var i = 0; i < okd.length; i++) {
if (sLst == okd[i]) {
return true
}
}
return aLst[1];
}
var ckValid = ckEmail(prompt('Enter your email address:'))
if (ckValid === true) {
alert(ckValid) // placeholder for process validated
} else {
alert(ckValid) // placeholder for show error message
}
</script>
<title></title>
</head>
<body>
</body>
</html>
回答by Markos
var rx = /^([\w\.]+)@([\w\.]+)$/;
var match = rx.exec("[email protected]");
if(match[1] == "yahoo.com"){
do something
}
second capturing group will contain the domain.
第二个捕获组将包含域。

