Splitting and Filtering

In this approach we will Split the string by space using str.split() method and then use filter() method filter out valid email formats.

Example: In this example, we will use split the string and filter out the required email substring.

Javascript
function extract(str) {
    const words = str.split(' ');
    const valid = words.filter(word => {
        return /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/.test(str);
    });
    return valid;
}

const str = 
    "My email address is info@w3wiki.org";
console.log(extract(str)); 

Output
[ 'My', 'email', 'address', 'is', 'info@w3wiki.org' ]

JavaScript Program to Extract Email Addresses from a String

In this article, we will explore how to extract email addresses from a string in JavaScript. Extracting email addresses from a given text can be useful for processing and organizing contact information.

There are various approaches to extract email addresses from a string in JavaScript:

Table of Content

  • Using Regular Expressions
  • Splitting and Filtering
  • Using String Matching and Validation
  • Using a Custom Parser

Similar Reads

Using Regular Expressions

Regular expressions provide an elegant way to match and extract email addresses from text....

Splitting and Filtering

In this approach we will Split the string by space using str.split() method and then use filter() method filter out valid email formats....

Using String Matching and Validation

In this approach, we will check each substring for email validity....

Using a Custom Parser

The custom parser approach iterates through words in the text, identifying email addresses by checking for the presence of ‘@’ and ‘.’ characters, and ensuring the structure resembles an email. This method is simple and doesn’t rely on regex....