简体   繁体   English

电子邮件地址的正则表达式匹配|| 一个字

[英]Regex match of email address || a word

I have an input field that will take a keyword input as either an email address or a plain text keyword. 我有一个输入字段,它将关键字输入作为电子邮件地址或纯文本关键字。 I just want to validate that there's no unusual or unsafe characters. 我只想确认没有异常或不安全的字符。 My normal validation expression I use for emails on the webform is: 我在网络表单上用于电子邮件的常规验证表达式是:

"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"

However, I need an expression that will also take a plain word such as "Blanket". 但是,我需要一个表达式也要使用简单的单词,例如“ Blanket”。 So, I need the expression to take all of the characters of a normal email address but not to validate the sequence that they're in. I've always been horrible at regular expressions. 因此,我需要该表达式接受普通电子​​邮件地址的所有字符,但不验证它们所在的顺序。正则表达式一直让我感到恐惧。 Thanks for your help, Chris. 克里斯,谢谢您的帮助。

Just do this: 只是这样做:

\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*|\w+

| means or . 意味着

So the above regex matches emails or plain words. 因此,上述正则表达式与电子邮件或纯文字匹配。

I have an input field that will take a keyword input as either an email address or a plain text keyword. 我有一个输入字段,它将关键字输入作为电子邮件地址或纯文本关键字。

You shouldn't be trying to validate e-mail addresses using a regexp. 您不应该尝试使用正则表达式来验证电子邮件地址。

The e-mail address format is covered by RFC 5322 and if you where to try and write a regexp that goes close to matching that spec, you would need a regexp that looks something like this: 电子邮件地址格式由RFC 5322涵盖,如果您在哪里尝试编写与该规范相符的正则表达式,则需要一个看起来像这样的正则表达式:

(((?:(?:(?:(?:(?:(?:[\\x20\\x09]*(?:\\x0d\\x0a))?[\\x20\\x09]+)|(?:[\\x20\\x09]+(?:(?:\\x0d\\x0a)[\\x20\\x09]+)* ))?(?:\\x28(?:(?:(?:(?:[\\x20\\x09]*(?:\\x0d\\x0a))?[\\x20\\x09]+)|(?:[\\x20\\x09]+(?:(?:\\x0d\\x0a)[\\x20...(click here to see full regexp) ((((?:(?:(?:(?:(?:(?:[\\ x20 \\ x09] *(?:\\ x0d \\ x0a))?[\\ x20 \\ x09] +)|(? \\ x20 \\ x09] +(?:(?:\\ x0d \\ x0a)[\\ x20 \\ x09] +)*)))?(?:\\ x28(?:(?:(?:(?:[?x20 \\ x09] *(?:\\ x0d \\ x0a))?[\\ x20 \\ x09] +)|(?:[\\ x20 \\ x09] +(?:(?:\\ x0d \\ x0a)[\\ x20 ...(点击此处查看完整的正则表达式)

The easiest way to validate an e-mail address is to send an e-mail to that address. 验证电子邮件地址的最简单方法是将电子邮件发送到该地址。

thanks for the question 谢谢你的问题

\w+([-+.']\w+)*@\w+([-.]*)

i hope this be helpful 我希望这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM