0

I want to match all the emails that are after the text "Email: "

However, when using match, the matches return the whole string: Email: demo@example.com.

Isn't there a way to just return the text matched by the parenthesis within the regular expression? (.*?) I just want to get the email address: demo@example.com

This is what I got so far:

var regex = new RegExp('E-mail:\\s(.*?)<br>', 'mg');
var matches = document.body.innerHTML.match(regex);

And yeap, I know using exec on a loop can retrieve on an array just the email. But I would have to manually create the loop and get what I want:

var regex = new RegExp('E-mail:\\s(.*?)<br>', 'mg');
var allMatches = [];
while( matches = regex.exec(document.body.innerHTML)){
    allMatches.push(matches[1]);
}
console.log(allMatches);
Alvaro
  • 40,778
  • 30
  • 164
  • 336

0 Answers0