Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a string contains an email address?

How can I check to verify that a given string contains an email address.

The email address would be included with a lot of other text, as well.

Also, not looking to necessarily strictly validate the email address itself. More so just wanting to make sure that [email protected] is present.

Example string:

Overall I liked the service, but had trouble using the widget generator.

Want more info? You can contact me at [email protected].

Plain javascript is fine, but I do happen to be using jQuery, so if there's some sort of helper function that makes this easier...go for it.

like image 1000
Shpigford Avatar asked May 07 '13 17:05

Shpigford


People also ask

How do you check if a string is an email address?

To check if a string is a valid email address in JavaScript, we can use a regex expression to match the symbols like @ , . and the strings in between them.

How do I check if a string contains text?

The includes() method returns true if a string contains a specified string. Otherwise it returns false . The includes() method is case sensitive.

How check string is email or not in PHP?

You can perform a PHP validation email by using the filter_var() function and passing the given email and filter id “FILTER_VALIDATE_EMAIL” as arguments.


3 Answers

Debuggex Example

JsFiddle Example

function checkIfEmailInString(text) { 
    var re = /(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/;
    return re.test(text);
}
like image 160
KingKongFrog Avatar answered Oct 13 '22 02:10

KingKongFrog


You can use this:

var StrObj = "whatever, this is my email [email protected] other text";
var emailsArray = StrObj.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
if (emailsArray != null && emailsArray.length) {
    //has email
}

This also lets you get the email address from the array, if you need it.

like image 40
dave Avatar answered Oct 13 '22 01:10

dave


Try

/\b[a-z0-9-_.]+@[a-z0-9-_.]+(\.[a-z0-9]+)+/i.test(text)
like image 23
Arun P Johny Avatar answered Oct 13 '22 02:10

Arun P Johny