0

I'm using this regex to match an "href" attribute in a <a> tag:

var href_matches = postRep.match(/href="(.*?)"/g);

The regex matches correctly the href except it returns the whole "href=http:example.com" string.
How do I manage to get only the href value (eg. "example.com")?

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
  • 1
    Possible duplicate of [JavaScript regular expressions and sub-matches](http://stackoverflow.com/questions/844001/javascript-regular-expressions-and-sub-matches) – Flint Sep 04 '16 at 19:16

3 Answers3

1

You can either run exec() on the regex :

var url_match = /href="(.*?)"/g.exec(postRep);

or remove the global flag

var url_match = postRep.match(/href="(.*?)"/);

Using String's match() function won't return captured groups if the global modifier is set.

Flint
  • 1,651
  • 1
  • 19
  • 29
1

Just another idea.

You can try something like this function:

function getHrefs(inputString) {
    var out = [];
    inputString.replace(/\bhref\b=['"]([^'"]+)['"]/gi, function(result, backreference) {
        out.push(backreference);
        return '';
    });
    return out;
}

Improved solution (much shortest):

function getHrefs(inputString) {
    return (inputString.match(/\bhref\b=['"][^'"]+(?=['"])/gi) || []).map(s => s.replace(/^href=["']/,""));
}

Edit:

There is other option - exec. But with exec you will need loop to get all matches (if you need this).

Georgi Naumov
  • 4,160
  • 4
  • 40
  • 62
0

You can use regex lookbehinds to check if the "href=" is there without actually including it in the match. For example, the regex (?<=href=)example\.com applied to href=example.com should only match example.com.

EDIT: This method only works in languages that support regex lookbehinds. Javascript doesn't support this feature. (thanks to Georgi Naumov for pointing this out)