0

Is there a jquery plugin or may be a regex which can be used to all the fields in query string of a mailto: link. It should support the mailto syntax.

Say if I have a link like ...

<a href="mailto:[email protected],[email protected]">Some msg</a>

The regex should give me the mail addresses in mailto: link - [email protected], [email protected]. It should also support links like

<a href="mailto:[email protected]?subject=Comments from MailTo Syntax Page">
<a href="mailto:[email protected]?body=I am having trouble finding information on">
<a href="mailto:[email protected]?subject=MailTo Comments&[email protected]&[email protected]">

For a link like

<a href="mailto:value">tell a friend</a>

I would pass value and the function and get an associative array of email addresses and other fields passed in query string.

4 Answers 4

4

Parsing URLs, and especially their query strings, is not a job for regexes. You could use a proper URL-parsing library or perhaps just a query string parsing one.

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

2 Comments

Really? hundreds of lines of code to pull out an email address?
Yes. Anyone who says otherwise doesn't understand the full complexity of URLs.
1

I'm not sure how accurate this is, but it might be improved if you feel a necessity to use regex.

function getEmails(str) {
// returns a list of email addresses in 'str'
    var tags = str.match(/<a href=('|")mailto:(.*?)\1(.*?)>/gi);
    var res = [];
    if (!tags.length) return res;
    for (var i = 0; i < tags.length; i++) {
        tags[i] = tags[i].replace(/^<a href=('|")mailto:(.+?)(\?[^\1]*)?\1>$/,'$2');
        var arr = tags[i].replace(/\s/g,'').split(',');
        for (var j = 0; j < arr.length; j++) res.push(arr[j]);
    }
    return res;
}

I'm not sure what you mean in the last part by the way so I tried to answer the first part.

1 Comment

I don't know if this satisfies the OP's needs, but this does exactly what I was after. Thanks! (Also thanks for not posting a "don't do this" answer :)
0

If you know what you are doing :

var href = 'mailto:[email protected]?subject=My+Subject&body=My+Body';

function getMailto(s) {
    var r = {};
    var email = s.match(/mailto:([^\?]*)/);
            email = email[1] ? email[1] : false;
  var subject = s.match(/subject=([^&]+)/);
            subject = subject ? subject[1].replace(/\+/g,' ') : false;
    var body = s.match(/body=([^&]+)/);
            body = body ? body[1].replace(/\+/g,' ') : false;

  if(email) {r['email'] = email;}
  if(subject) {r['subject'] = subject;}
  if(body) {r['body'] = body;}

  return r;
}

console.log(getMailto(href));

Demo : https://jsfiddle.net/l2aelba/qov9wpk5/

3 Comments

+1 for adding a solution rather than saying you shouldn't do it! , although the method name in the console.log in inconsistent, and needs to be stringified.
-1 This parsing is completely insufficient for URL decoding in general. For example, if the subject contains an exclamation mark, it will have been encoded as %21, which your code would leave as-is.
@Anko This is just "use on your own risk!"
0

This works for me.

function parseMailTo(mailto) {
  var url = new URL(mailto)
  var query = parseQuery(url.search)
  return {
    to: {url.pathname}
    subject: {query.subject}
  }
}

function parseQuery(queryString) {
  var query = {};
  var pairs = (queryString[0] === '?' ? queryString.substr(1) : queryString).split('&');
  for (var i = 0; i < pairs.length; i++) {
      var pair = pairs[i].split('=');
      query[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || '');
  }
  return query;
}

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.