1

I would like to get from an URL like these JUST the "test@gmail.com" (or whatever email will be in the URL) :

  • www.website.com/?email=test@gmail.com
  • www.website.com/?email=test@gmail.com&name=john
  • www.website.com/?name=john&email=test@gmail.com

My problem is to solve that all of these 3 cases can happen (so no other data, OR data after the email, OR data before the email).

Unfortunately I have zero Regex-Skills, but was able to get so far together these 2 things:

/email=(.*)/
/email=(.*)\&/

The 1st one works if the email is the ONLY data. The 2nd one works just if there is another data after my email.

But as mentioned I need to make it work regardless of which of the 3 types above is the case.

Could you help me out please?

Thank you very much!

BTW, I need it for this:

var msg = window.location.href.match(/email=(.*)/);

(I'm searching for an answer for more than 2 hours, used Google, used Regex-Testers, checked out Cheat Sheets and read many StackOverFlow-Questions... But I can't solve it on my own.)

Imre
  • 23
  • 6
  • Possible duplicate of [Regex for email matching](http://stackoverflow.com/questions/11312662/regex-for-email-matching) – Marcs Dec 01 '16 at 01:01

2 Answers2

1

Try this regex: https://regex101.com/r/gMyqDa/1 It matches email patterns not just any characters after email=

yelsayed
  • 5,236
  • 3
  • 27
  • 38
  • Thank you, but this has been deleted (and I think I deleted it, as I see now that you gave me a delete-link... :) ) – Imre Nov 30 '16 at 23:53
  • 1
    Well that's embarrassing! Fixed. :) – yelsayed Nov 30 '16 at 23:59
  • Wonderful, thank you very much yBot! I implemented this in my site and it's working fine :) Thats a much easier approach than my tries – Imre Dec 01 '16 at 00:21
0

if your cases are that simple, then just exclude the ampersand

var urls = [
"www.website.com/?email=test@gmail.com",
"www.website.com/?email=test@gmail.com&name=john",
"www.website.com/?name=john&email=test@gmail.com"
];

for (i in urls) {
  var url = urls[i];
  console.log(url);
  console.log(url.match(/email=[^&]+/)[0]);
}
arhak
  • 2,488
  • 1
  • 24
  • 38