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?
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 /<(.*)>/
:
/
and /
are mandatory to define a regex<
and >
simply matches the two <
and >
in your string()
parenthesis "capture" what you're looking for. Here, they get the mail address inside..*
: .
means "any character", and *
means "any number of times. Combined, it means "any character any number of times", and that is inside <
and >
, which correspond to the place where the mail is.