Reputation: 59
I have this structure:
<ul>
<li><a href="mydomain/site/location/paris/">Paris</a></li>
<ul>
<li><a href="mydomain/site/location/london/">London</a></li>
</ul>
<li><a href="mydomain/site/location/amsterdam-and-paris/">Amsterdam and Paris</a></li>
</ul>
and i need to replace href of each anchor with this:
<a href="mydomain/site/event?location=london">London</a>
Can you help me please
Upvotes: 1
Views: 4293
Reputation: 78671
.attr()
has a form when you can use a callback function to transform the currently set attribute.
Using it and combining with the right regex something like this would work:
$("a[href^='mydomain/site']").attr('href', function (i, attr) {
return attr.replace(
/^(mydomain\/site\/)(location)\/([^/]*)\//
, '$1event?$2=$3'
);
})
Upvotes: 10