JavaScript Regular Expressions and Pattern matching within Strings Email Extract

JavaScript Regular Expressions and Pattern matching within Strings Email Extract

Regexp with match and matchAll() methods 1
Use of test() and exec() 3
replace() and replaceAll() methods 4
search() method 4
Find Email Pattern 6
Email Extractor Project 8
How to create a text file download 12

Regexp with match and matchAll() methods

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

const str1 = ‘Hello World. I love JavaScript!’;
txtArea.value = str1;

const rexg = /love/g;
const rex = /love/;

btn.addEventListener(‘click’,()=>{
const txt = txtArea.value;
const finder1 = txt.match(rex);
console.log(finder1);
const finder2 = txt.match(rex);
console.log(finder2);
const finder = […txt.matchAll(rexg)];
console.log(finder);
let html = ”;
if(finder!=null) {
finder.forEach((r,i)=>{
html += <div>Found ${r[0]} at index value ${r.index}</div>;
})
}else{
html = ‘Not found in the string’;
}
res.innerHTML = html;
})



JavaScript

Checker

Use of test() and exec()
const txtArea = document.querySelector(‘textarea’);
const btn = document.querySelector(‘button’);
const res = document.querySelector(‘.results’);

const str1 = ‘Hello love World. I love JavaScript!’;
txtArea.value = str1;

//const rexg = /love/g;
const rexg = RegExp(‘love’,’g’);

btn.addEventListener(‘click’,()=>{
const txt = txtArea.value;
//const res = rexg.test(txt);
while((arr = rexg.exec(txt)) !== null){
console.log(found at starting index ${arr.index} ending index ${rexg.lastIndex});
}
const res = rexg.exec(txt);
console.log(res);
})

replace() and replaceAll() methods

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

const str1 = ‘Hello love World. I love JavaScript!’;
const rexg = RegExp(‘love’,’g’);
const reg = /love/gi;
const reg1 = /love/g;
txtArea.value = str1;

btn.addEventListener(‘click’,()=>{
const str = txtArea.value;
const str2 = str.replace(reg,’enjoy’);
const str3 = str.replaceAll(reg1,’fun’);
const str4 = str.replaceAll(‘love’,’fun’);
const str5 = str.replace(‘love’,’like’);
res.innerHTML = str3;
console.log(str);
})

search() method

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

const str1 = ‘Hello love World. I love JavaScript!’;
const reg = /love/i;
txtArea.value = str1;

btn.addEventListener(‘click’,()=>{
const str = txtArea.value;
const rep = str.search(reg);
if(rep == -1){
res.textContent = Result ${reg} not found in the string!;
}else{
res.textContent = Result ${reg} found at index value ${rep};
}
console.log(rep);
})

Find Email Pattern

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;

btn.addEventListener(‘click’,()=>{
const str = txtArea.value;
const contents = str.match(regEx);
console.log(contents);
if(contents == null){
res.innerHTML = ‘No match was found!’;
}
else{
res.innerHTML = ”;
const holder = [];
contents.forEach(email =>{
if(!holder.includes(email)){
holder.push(email);
const el = document.createElement(‘div’);
el.textContent = email;
res.append(el);
}
})
}
})

Email Extractor Project




JavaScript
Add Text ContentEmail separator , – space _ ; Get Emails

const txt1 = document.querySelector(‘#txtContent’);
const btn = document.querySelector(‘button’);
const res = document.querySelector(‘.results’);
const sel = document.querySelector(‘#sep’);
//const str = ‘svekis.laurence@example.com test hello world svekis-1@example.com svekis_2@example.com’;
//txt1.value = str;

btn.addEventListener(‘click’,getEmails);

function getEmails(){
const temp = txt1.value;
const regexp = /([A-Za-z0-9.-]+@[A-Za-z0-9.-]+.[A-Za-z0-9._-]+)/gi;
const emailsRaw = temp.match(regexp);
const emails = [];
if(emailsRaw != null ){
emailsRaw.forEach(email =>{
if(!emails.includes(email)){
emails.push(email);
}
});
}
const main = document.createElement(‘div’);
main.classList.add(‘box’);
res.append(main);
const div = document.createElement(‘div’);
div.textContent = Found emails : (${emails.length});
main.append(div);
if(emails.length > 0){
const txta= document.createElement(‘textarea’);
const myEmails = emails.join(sep.value);
txta.value = myEmails;
main.append(txta);
txta.focus();
txta.addEventListener(‘click’,()=>{
console.log(‘selected’);
txta.select();
})
downBtn = document.createElement(‘button’);
downBtn.textContent = Download ${emails.length} Emails;
downBtn.classList.add(‘btn’);
main.append(downBtn);
downBtn.addEventListener(‘click’,()=>{
const fileName = ‘Emails.txt’;
const a = document.createElement(‘a’);
a.setAttribute(‘href’,’data:text/plain;charset=utf-8,’+encodeURIComponent(myEmails));
a.setAttribute(‘download’,fileName);
a.style.display=’none’;
document.body.append(a);
a.click();
document.body.removeChild(a);
})
}
console.log(emailsRaw);
console.log(emails);
}

How to create a text file download

function maker(){
const fileName = ‘myFile.txt’;
const a = document.createElement(‘a’);
a.setAttribute(‘href’,’data:text/plain;charset=utf-8,’+encodeURIComponent(‘hello world’));
a.setAttribute(‘download’,fileName);
a.style.display=’none’;
document.body.append(a);
a.click();
document.body.removeChild(a);
}