简体   繁体   中英

Find everything that's not an email address using only regex

I need to find everything in a string that is not an e-mail address.

Here is my version of how to find an e-mail address.

^[a-zA-Z0-9_.-]+@[a-zA-Z0-9][a-zA-Z0-9-.]+\.([a-zA-Z]{2,6})$

I want to modify this regex to find the inverse--everything other than the e-mail address in any string.


###Example 1: asdasd

###Example 2: 123@asd.com sda


Note: I want to get status == true in the following line:

var status = myString.match(pattern matches everything that is not an email address);

###I can only change the pattern, nothing else!

The official standard is known as RFC 2822 . Regex pattern for email address is then:

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

More practical implementation of RFC 2822 (if we omit the syntax using double quotes and square brackets), which will still match 99.99% of all email addresses in actual use today, is:

[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

To get list of non-matching "words" from myString use JavaScript code:

var status = myString.match(/(?:\s|^)(?![a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\S+\b(?=\s|$)/ig);

Check this demo .

You can call replace , with an empty string to remove the instances of the emails matching your pattern:

var noEmails = stringWithEmails.replace(/[a-zA-Z0-9_.-]+@[a-zA-Z0-9][a-zA-Z0-9-.]+\.([a-zA-Z]{2,6})/g, '');

Just note that I took out the leading ^ and trailing $ . These were forcing the pattern to match the whole string ( ^ is beginning of line, and $ is end of line).

var result=myString.replace('[a-zA-Z0-9_.-]+@[a-zA-Z0-9][a-zA-Z0-9-.]+\.([a-zA-Z]{2,6})', '');

string.replace(/[/[a-zA-Z0-9_.-]+@[a-zA-Z0-9][a-zA-Z0-9-.]+.([a-zA-Z]{2,6})]*/gi, '');

its worked

Based on your edits, what you actually want is

var status = ! myString.match(email pattern)

That is, use your email pattern (or the giant thing posted by Ωmega) with the guards. That will match everything that is only an email address. That's exactly the opposite of what you want, so invert the boolean and you're done.

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