JAVASCRIPT
Extract All Email Addresses from Text
Learn how to use JavaScript regex to efficiently find and extract all valid email addresses present within any text string for data processing.
const text = "Contact us at [email protected] or [email protected] for assistance.";
const emailRegex = /\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b/g;
const emails = text.match(emailRegex);
console.log(emails);
How it works: This snippet uses a regular expression to match common email address formats within a given string. The `match()` method, combined with the global flag `g` in the regex, ensures that all occurrences of valid email addresses are found and returned as an array. The pattern accounts for alphanumeric characters, periods, underscores, percent, plus, and hyphens before the '@' symbol, followed by a domain name and a top-level domain.