Forum Discussion

PerBonomi's avatar
4 years ago

RegEx replace hyperlink

I wonder if someone can help me with this one, since I'm having limited success.

I'm trying to turn this

<a class="cust-a" title="linktitle" href="http://thisismyurl.com" alt="alt here" >link text</a>

into this

[link text|http://thisismyurl.com]

 

This gets me the link text, but apart from that I'm stuck. And I'd really like to do both of them in one replace, if possible.

str?replace("(?i)<a([^>]+)>(.+?)<\\/a>","$2","rgmi")
  • The answer always comes to you after you ask out loud. so in case someone's wondering:

    ?replace('(<a[^>]+href=\"(.*?)\"[^>]*>(.+)(<\\/a>))','[$3|$2]','rgmi')
  • Does it have to be RegEx? It's going to be pretty complicated if so.

    HTML isn't a "regular" language, so capturing it with a Regular Expression isn't ideal.

    Does this help? The link format you're aiming for isn't markdown, but it's close.