2

I need to extract a single email address from this kind of string.

Unauthorized: Your password has expired. We have sent a reset password link to example@gmail.com. Please check your email for details

const string = "Unauthorized: Your password has expired. We have sent a reset password link to example@gmail.com. Please check your email for details";
const mailMatch = string.match(/(\S+@[^\s.]+\.{1}[^.]\S+)/);

The match in this case will be this `

[0: "example@gmail.com."
1: "example@gmail.com."
groups: undefined
index: 79
input: "Unauthorized: Your password has expired. We have sent a reset password link to example@gmail.com. Please check your email for details"
length: 2]

I don't want to match the dot(indicating end of the sentence) at the end of mail. How to change my regexp , in order to get only example@gmail.com

Norayr Ghukasyan
  • 1,298
  • 1
  • 14
  • 28

1 Answers1

2

You may use

var string = "Unauthorized: Your password has expired. We have sent a reset password link to example@gmail.com. Please check your email for details";
var mailMatch = string.match(/\S+@[^\s.]+\.[^.\s]+/);
console.log(mailMatch); // => Matched text: example@gmail.com
// Or, if you may have any non-whitespace chars and you want to stop at the last
console.log(
  "The example@site.co.uk address is not available".match(/\S+@[^\s.]+\.\S+\b/)
); // => Matched text: example@site.co.uk
// Or just
console.log(
  "The example@some.site.co.uk address is not available".match(/\S+@\S+\.\S+\b/)
); // => Matched text: example@some.site.co.uk

Since it is not quite clear what email requirements you have a more generic example would be

s.match(/\S+@\S+\.\S+\b/)

Details

  • \S+ - 1+ non-whitespace chars
  • @ - a @ char
  • \S+ - 1+ non-whitespace chars
  • \. - a dot
  • \S+\b - 1+ non-whitespace chars that end with a word boundary.

If you need to extract valid looking emails only here is a solution with a bit amended well-known email validation regex:

var email_rx_extract = /(?:[^<>()[\]\\.,;:\s@"]+(?:\.[^<>()[\]\\.,;:\s@"]+)*|".+")@(?:\[\d{1,3}(?:\.\d{1,3}){3}]|(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,})(?![a-zA-Z])/g;
var s = "The example@some.site.co.uk address is not available\nBad email is example@gmail...........com.";
var results = s.match(email_rx_extract);
console.log(results); // => Only example@some.site.co.uk is found.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • I don't agree with you , because in your case it will retrieve also `example@gmail...........com` – Norayr Ghukasyan Aug 22 '19 at 13:34
  • @NorayrGhukasyan Well, if you need validation, that is a different story. If you are extracting from a legit source, you should not have such weird stuff. If you have `\S+@` (that also matches `///...,,,abc...@`) you can have `\S+\.\S+\b` after. Else, you should really think of what you are doing. – Wiktor Stribiżew Aug 22 '19 at 13:35
  • @NorayrGhukasyan Actually, you may use a very simple regex like my last one, to extract all email-looking strings, and then use an email validation check, to filter the results. – Wiktor Stribiżew Aug 22 '19 at 13:39