I need to check an anchor path for containing /wp-content/ .
For instance, <a href="http://domain.com/wp-content/...">Link1</a>
In this case the RegExp must match pattern with url and return string in array
['http://domain.com', 'domain.com', 'http://domain.com/wp-content/', '']
In my code I use this but I do something wrong
$('a').live('click', function(e) {
var host = thisClick.attr('href'),
pattern = /(?:https?://)?(?:www.)?(.*?)?(/wp-content/)?//,
matchReg = host.match(pattern);
});
What’s wrong with up line code?
See what happens when you apply your regex:
<
, doesn’t matchh
in(?:https?://)?
. Moving on.w
in(?:www.)?
, moving on.
in(.*?)?
/
or a/wp-content/
<a href="http:/
matched.You can’t make everything optional, or there isn’t any anchor the regex can work on to get to the start of the URL. If you’re trying to always match something in a
<a>
tag, try:/wp-content/
, that’s your criteria.wp-content/
http://domain.com/wp-content/
, secondhttp://domain.com
, thirddomain.com