javascript 如何从电子邮件地址检索姓名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18371339/
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
How to retrieve Name from Email Address
提问by leeshin
With javascript, how do we remove the @gmail.com or @aol.com from a string so that what only remains is the name?
使用javascript,我们如何从字符串中删除@gmail.com 或@aol.com,以便只剩下名称?
var string = "[email protected]";
Will be just "johdoe"? I tried with split but it did not end well. thanks.
会只是“johdoe”吗?我尝试了 split 但它并没有很好地结束。谢谢。
回答by Roko C. Buljan
var email = "[email protected]";
var name = email.substring(0, email.lastIndexOf("@"));
var domain = email.substring(email.lastIndexOf("@") +1);
console.log( name ); // john.doe
console.log( domain ); // email.com
The above will also work for validnames containing @
(tools.ietf.org/html/rfc3696Page 5)
以上也适用于包含(tools.ietf.org/html/rfc3696 Page 5)的有效名称@
john\@doe
"john@@".doe
"j@hn".d\@e
john\@doe
"john@@".doe
"j@hn".d\@e
回答by Joseph Myers
You should take note that a valid email address is an incredibly sophisticated object and may contain multiple @
signs (ref. http://cr.yp.to/im/address.html).
您应该注意,有效的电子邮件地址是一个非常复杂的对象,可能包含多个@
符号(参考http://cr.yp.to/im/address.html)。
"The domain part of an address is everything after the final
@
."
“地址的域部分是最终
@
.之后的一切。”
Thus, you should do something equivalent to:
因此,您应该执行以下操作:
var email = "[email protected]";
var name = email.substring(0, email.lastIndexOf("@"));
or even shorter,
甚至更短,
var name = email.replace(/@[^@]+$/, '');
If you want both the name and the domain/hostname, then this will work:
如果您想要名称和域/主机名,那么这将起作用:
var email = "[email protected]";
var lasta = email.lastIndexOf('@');
var name, host;
if (lasta != -1) {
name = email.substring(0, lasta);
host = email.substring(lasta+1);
/* automatically extends to end of string when 2nd arg omitted */
} else {
/* respond to invalid email in some way */
}
回答by Wtower
And another alternative using split:
使用split 的另一种选择:
var email = "[email protected]";
var sp = email.split('@');
console.log(sp[0]); // john.doe
console.log(sp[1]); // email.com
回答by Atish Dipongkor - MVP
Try it using substring()
and indexOf()
尝试使用substring()
和indexOf()
var name = email.substring(0, email.indexOf("@"));
回答by Atish Dipongkor - MVP
var email = "[email protected]";
email=email.replace(/@.*/,""); //returns string (the characters before @)
回答by Manoj Kadolkar
You can try with the replace() and regular expression. You can read more about replace() using regex here
您可以尝试使用 replace() 和正则表达式。您可以在此处使用正则表达式阅读有关 replace() 的更多信息
var myEmail = '[email protected]';
var name= myEmail.replace(/@.*/, "");
console.log(name);
This returns the string before @
这将返回@ 之前的字符串
回答by Jonth Sm
As shown in How to remove or get domain name from an e-mail address in Javascript?, you can do it using the following code:
如如何在 Javascript 中从电子邮件地址中删除或获取域名?,您可以使用以下代码执行此操作:
const getDomainFromEmail = email => {
let emailDomain = null;
const pos = email.search('@'); // get position of domain
if (pos > 0) {
emailDomain = email.slice(pos+1); // use the slice method to get domain name, "+1" mean domain does not include "@"
}
return emailDomain;
};
const yourEmail = "[email protected]"
console.log(getDomainFromEmail(yourEmail));
// result : 4codev.com