1

I would like to get from an URL like these JUST the "[email protected]" (or whatever email will be in the URL) :

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.)

1

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, but this has been deleted (and I think I deleted it, as I see now that you gave me a delete-link... :) )
Well that's embarrassing! Fixed. :)
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
0

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

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

for (i in urls) {
  var url = urls[i];
  console.log(url);
  console.log(url.match(/email=[^&]+/)[0]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.