Salvatore Tempesta Salvatore Tempesta - 1 year ago 60
AngularJS Question

Regex do not match content but whole searched string

I'm using this regex to match an "href" attribute in a


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

The regex matches correctly the href except it returns the whole "" string.

How do I manage to get only the href value (eg. "")?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download