javascriptregexstring

Extract email address from string


I have a string like this:

Francesco Renga <francesco_renga-001@gmail.com>

I need to extract only the email, i.e. francesco_renga-001@gmail.com.

How can I do this in nodejs/javascript in "elegant" way?


Solution

  • Here's a simple example showing how to use regex in JavaScript :

    var string = "Francesco Renga <francesco_renga-001@gmail.com>"; // Your string containing
    var regex = /<(.*)>/g; // The actual regex
    var matches = regex.exec(string);
    console.log(matches[1]);

    Here's the decomposition of the regex /<(.*)>/ :