简体   繁体   中英

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". 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:

(((?:(?:(?:(?:(?:(?:[\\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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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