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