Wrap <a> tag around URLs

207 views
Skip to first unread message

Martin

unread,
Feb 4, 2021, 2:24:48 PM2/4/21
to BBEdit Talk
Hi,
this is my first post to this group. I hope I can express myself clearly ...

Can I use BBEdit to automatically convert lots of web addresses (e.g. https://test.com) in an html document into hyperlinks? Perhaps by assigning a <a> tag with "Search and replace" so that something like this arises: <a href="https://test.com" target="_blank"> https://test.com </ a >?

Fletcher Sandbeck

unread,
Feb 4, 2021, 2:44:02 PM2/4/21
to bbe...@googlegroups.com
You can use Find/Replace to do this. Open the Find dialog from the Search menu, check the "grep" box at the bottom which allows you to use regular expressions, and use these patterns to start. The first find https:// followed by the characters that are commonly found in URLs. The second replaces that with an anchor tag with the found text \0 used twice.

Find: https?://[a-zA-Z0-9/:\?&%=._\-+]+
Replace: <a href="\0" target="_blank">\0</a>

Find the first entry and if it needs replacing choose Replace and Find Next or simply Find Next if it doesn't need replacing. Or, if you don't have any links that are already formatted in the document you can do Replace All. I have key shortcuts assigned to these so I can quickly work through a document.

The Pattern Playground on the Search menu is useful for playing around with the find/replace patterns as well.

Hope this helps,

[fletcher]

--
This is the BBEdit Talk public discussion group. If you have a feature request or need technical support, please email "sup...@barebones.com" rather than posting here. Follow @bbedit on Twitter: <https://twitter.com/bbedit>
---
You received this message because you are subscribed to the Google Groups "BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bbedit+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bbedit/5d8eea22-1b3c-49ca-8ced-202402d196ean%40googlegroups.com.

Martin

unread,
Feb 4, 2021, 4:28:04 PM2/4/21
to BBEdit Talk
That's great! Thank you fletcher. That saved me a lot of work. I think this is the beginning of a love story between grep and me :-)

Christopher Finazzo

unread,
Feb 7, 2021, 10:54:58 AM2/7/21
to BBEdit Talk
I have a similar situation and am wondering if this would work...

The Find pattern you described will indeed find all of the URLs in my document, but if I want to replace each of them with the same root URL, (So, instead of href="\0", I could use href="example.com") is this something I could modify the script to do?

Christopher Stone

unread,
Feb 7, 2021, 11:26:01 AM2/7/21
to BBEdit-Talk
On 02/07/2021, at 09:54, Christopher Finazzo <chris....@gmail.com> wrote:
I have a similar situation and am wondering if this would work...

The Find pattern you described will indeed find all of the URLs in my document, but if I want to replace each of them with the same root URL, (So, instead of href="\0", I could use href="example.com") is this something I could modify the script to do?


Hey Christopher,

That should be simple enough.

Please provide a couple of real-world before and after examples of what you want to find/replace.

Seeing actual examples makes it much easier to construct a proper regular expression.

--
Best Regards,
Chris

Christopher Finazzo

unread,
Feb 7, 2021, 2:53:14 PM2/7/21
to BBEdit Talk
The URL's I am thinking of come from OpenLibrary. Currently on my site I have a list of books - represented by URLs in a YAML file - with Amazon links for each one. With the exception of a few (because Audible), these can be migrated to use OL instead.

The Amazon URL's are of the format: https://www.amazon.com/Kitchen-Confidential-Adventures-Culinary-Underbelly/dp/158234082X/ - so the root URL, a name, slash "dp", and an identifier.

OpenLibrary's URL's are similar: E.g, https://openlibrary.org/works/OL18147912W/Fear_Trump_in_the_White_House - A root URL, slash "works", an identifier, and a name.

Does it matter that this is a wholesale replacement of some lines?
Reply all
Reply to author
Forward
0 new messages