Search
Other ReReplacer questions
Forum

Regex Request: Extract link href value and link display text

Nate Covington's Avatar Nate Covington
This is what I'm starting with - find:
<a href="([^"#]*)"

Replace:
<a href="\1" onclick="ga('send','event', {'eventCategory': 'Button','eventAction': 'Click', 'eventLabel':'\1'});"

I want to replace "eventAction=Click" with the text of the actual link.

Something more like find:
<a href="([^"]*)"*</a>

Replace:
<a href="\1" onclick="ga('send','event', {'eventCategory': 'Button','eventAction': '\2', 'eventLabel':'\1'});">\2

Where "\2" is my magic variable for the actual visible link text - what appears before </a>

And the tricky part is sometimes my links are structured like this:
<a href="/url" onclick="ga('send','event', {'eventCategory': 'Button','eventAction': 'Click', 'eventLabel':'/url'});"><span class="orange"><span style="text-decoration: underline;"> link text here</span></span></a>

Thanks!
Peter van Westen's Avatar Peter van Westen ADMIN
Can you give a few examples of the full before HTML of the links you have and after HTML you want?
And do you want this added on every single link in your website? What happens to links that already have an onclick?
Please post a rating at the Joomla! Extensions Directory
Nate Covington's Avatar Nate Covington
Sure, here's a messy one, before:
<a href="/article-url-here"><span class="orange"><span style="text-decoration: underline;">link text here</span></span></a>

After:
<a onclick="ga('send','event', {'eventCategory': 'Button','eventAction': 'link text here, 'eventLabel':'/article-url-here'});" href="/article-url-here"><span class="orange"><span style="text-decoration: underline;">link text here</span></span></a>

And a cleaner one - before:
<a href="/article-url-here">link text here</a>

After:
<a onclick="ga('send','event', {'eventCategory': 'Button','eventAction': 'link text here, 'eventLabel':'/article-url-here'});" href="/article-url-here">link text here</a>
Peter van Westen's Avatar Peter van Westen ADMIN
Ok, and what about links with classes and other attributes? Should they also be done?
What about links with other tags inside, like strong and em and images?
Please post a rating at the Joomla! Extensions Directory
Peter van Westen's Avatar Peter van Westen ADMIN
It all makes the regex very complex for this kind of thing...
Please post a rating at the Joomla! Extensions Directory
Nate Covington's Avatar Nate Covington
Other link classes should be okay, it should process those. Can it skip any links that already has an "onclick"? For now let's make it only apply to text images, anything with an image can be skipped too. Whatever makes this easy is a good place to start, from my angle.
Peter van Westen's Avatar Peter van Westen ADMIN
Something like this:
<a href="[^"]*)">((?:<span[^>]*>)*([^<]+?)(?:</span>)*)</a>
Replace with:
<a href="\1" onclick="ga('send','event', {'eventCategory': 'Button','eventAction': '\3', 'eventLabel':'\1'});">\2</a>
Please post a rating at the Joomla! Extensions Directory
Nate Covington's Avatar Nate Covington
Hmm, that doesn't seem to be firing.
Nate Covington's Avatar Nate Covington
Can you write it so it targets just the "clean" version, ignore the spans and nested junk?

If we could get it working just for this - before:
<a href="/article-url-here">link text here</a>

After:
<a onclick="ga('send','event', {'eventCategory': 'Button','eventAction': 'link text here, 'eventLabel':'/article-url-here'});" href="/article-url-here">link text here</a>
Peter van Westen's Avatar Peter van Westen ADMIN
Sorry, I was missing a parenthesis:
<a href="([^"]*)">((?:<span[^>]*>)*([^<]+?)(?:</span>)*)</a>
Please post a rating at the Joomla! Extensions Directory
Nate Covington's Avatar Nate Covington
YOU'RE A WIZARD!!!
Peter van Westen's Avatar Peter van Westen ADMIN
?
Please post a rating at the Joomla! Extensions Directory
You can only post on the extension support forum if you have an active subscription and you log in

Buy a Pro subscription