Skip to content

Regex to capture the group

I have a text where I want to capture email with line starting from “Email: ******”

Details of Contact Made: 
Sender's Name: Test (Individual) 
Mobile: 4354354354 
Email: [email protected] 
Message: I am interested in your property. Please get in touch with me 
Click here 
<https://www.magicbricks.com/bricks/mailerautologin.html?

I have tried with .match(/Email:(.*)1/g)

But I am getting [Email:] instead of [email protected] How do I get the matching email from the group

Advertisement

Answer

You need to access the first capture group. But I would use this version:

var text = `Details of Contact Made: 
Sender's Name: Test (Individual) 
Mobile: 4354354354 
Email: [email protected] 
Message: I am interested in your property. Please get in touch with me 
Click here 
<https://www.magicbricks.com/bricks/mailerautologin.html?`;

var email = text.match(/Email:s*(S+?@S+)/)[1];
console.log(email);