Reputation: 4511
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:
[email protected] 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!
Upvotes: 2
Views: 5203
Reputation: 43663
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.
Upvotes: 3
Reputation: 24473
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.
Upvotes: -1
Reputation: 1509
string.replace(/[/[a-zA-Z0-9_.-]+@[a-zA-Z0-9][a-zA-Z0-9-.]+.([a-zA-Z]{2,6})]*/gi, '');
its worked
Upvotes: 0
Reputation: 1325
var result=myString.replace('[a-zA-Z0-9_.-]+@[a-zA-Z0-9][a-zA-Z0-9-.]+\.([a-zA-Z]{2,6})', '');
Upvotes: 0
Reputation: 5303
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).
Upvotes: 0