Salvatore Tempesta Salvatore Tempesta - 2 months ago 6
AngularJS Question

Regex do not match content but whole searched string

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")?

Answer

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.