regex to add target property to end of external links

This forum is for all Flare issues related to importing files or projects.
Post Reply
cayennep
Sr. Propeller Head
Posts: 390
Joined: Mon Jul 26, 2010 3:42 pm

regex to add target property to end of external links

Post by cayennep »

I want to find all instances of external links
this works in regex, to find the string:
<a href="http(.*)">

however, I can't find the thing to replace it, just changing it to <a href="http(.*)" target="blank">
(appending the target property between the quote and chevron, or even before it would work)

also, anybody know what it would be in FAR? I used to use it a lot but it didn't seem to work as well at some point. I can't even figure out the first part, find, in FAR so maybe the regex replace string would be better, since I'm halfway there.

Thanks kindly
Psider
Propellus Maximus
Posts: 811
Joined: Wed Jul 06, 2011 1:32 am

Re: regex to add target property to end of external links

Post by Psider »

This works in Notepad++. The general idea should be the same in most regex tools, although the syntax might vary depending on the tool.

Find: (<a href="http.*?")(>)
Replace: \1 target="_blank"\2

The parentheses in the Find statement create groups which can be referenced by number in the replace (\1, \2, etc). This one has two groups (I took the parentheses out of your original as I don't think they're needed, but again, this is not using Flare, so you may have to experiment.)

The ? after the * means "lazy" so the wildcard match will match the minimum characters rather than as many as possible. I suspect it isn't entirely necessary in this example, but it doesn't hurt to get in the habit of including it. For example, using just .* I've sometimes accidentally matched more than just the single tag I was intending.

In the replace, we're saying to copy in the original group one text, add a space, then the specified text, then copy in the original group two text.

e.g.
<a href="www.google.com"> becomes <a href="www.google.com" target="_blank">

I advise experimenting on a small topic, because there are variations in regex implementation. For example, some use $1, $2, etc in the Replace statement. Also, you may need to turn on a setting so that the dot matches carriage returns (technically newlines) - usually it doesn't, so if the text is split across lines, then it won't be found.

I think Flare didn't used to support the group references used (\1), but it's possible that has changed in newer versions.

I would recommend using FAR if you have it, and check the help file for how they implement regex.

Hope that helps.
NorthEast
Master Propellus Maximus
Posts: 6359
Joined: Mon Mar 05, 2007 8:33 am

Re: regex to add target property to end of external links

Post by NorthEast »

OK, this isn't using regex, but I handle the same thing in my help by using a script.

So this would find all a links with an href starting with http, and then set the target attribute to blank (i.e. target='_blank'):

Code: Select all

$( document ).ready(function() {
		$('a[href^="http"]').attr('target', '_blank'); 
});
For my help, I also add an class called "external", so I can format the links slightly differently:

Code: Select all

$( document ).ready(function() {
		/* find all external links and add 'external' class and '_blank' target (force it to open in new window) */
		$('a[href^="http"]').addClass('external').attr('target', '_blank'); 
});
cayennep
Sr. Propeller Head
Posts: 390
Joined: Mon Jul 26, 2010 3:42 pm

Re: regex to add target property to end of external links

Post by cayennep »

Psider wrote:This works in Notepad++. The general idea should be the same in most regex tools, although the syntax might vary depending on the tool.
Thanks! The explanation gives me some tips, if I still try to do it this way. All the tools are different, so that adds challenge, but understanding the principles is really helpful. I have to edit/clean up each topic anyway, so may just do it manually.

Unfortunately, I've not been able to get FAR to work properly for years, tho i used to use it all the time for import cleanups.

And, I always 1) back up the whole content folder I'm modifying before doing this kind of thing and 2) have a topic open in sublime or whatever text editor so I can see what it's doing. Good practice!
cayennep
Sr. Propeller Head
Posts: 390
Joined: Mon Jul 26, 2010 3:42 pm

Re: regex to add target property to end of external links

Post by cayennep »

Psider wrote:This works in Notepad++. The general idea should be the same in most regex tools, although the syntax might vary depending on the tool.

Find: (<a href="http.*?")(>)
Replace: \1 target="_blank"\2
Thanks so much! I just used Notepad++, any complete suggestion I was glad to go that route!!
This worked brilliantly, much appreciated!
Psider
Propellus Maximus
Posts: 811
Joined: Wed Jul 06, 2011 1:32 am

Re: regex to add target property to end of external links

Post by Psider »

I'm glad it worked. It's taken me forever to understand the more complex regex and I still count myself a hack beginner. :)
Post Reply