Learn JavaScript coding lesson How to use Regular Expression RegExp to find Email Pattern in string

Regular expressions provide a powerful way to create patterns that can be matched from string content in JavaScript code. To find all matches of a typical email there are several ways to create the pattern. Start by setting up a grouping of the patterns, using the () adding the global flag to match all the results. This can still be used to find a word or pattern within your string.

const txtArea = document.querySelector(‘textarea’);
const btn = document.querySelector(‘button’);
const res = document.querySelector(‘.results’);

const str1 = ‘Hello World. rr=@ 123456 svekis.1@example.com I love JavaScript! svekis_2@example.com svekis-2@example.com’;
txtArea.value = str1;
const regEx = /([A-Za-z0-9.-]+@[A-Za-z0-9.-]+.[A-Za-z0-9._-]+)/g;

const str = txtArea.value;
const contents = str.match(regEx);
if(contents == null){
res.innerHTML = ‘No match was found!’;
res.innerHTML = ”;
const holder = [];
contents.forEach(email =>{
const el = document.createElement(‘div’);
el.textContent = email;