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
regex to add target property to end of external links
Re: regex to add target property to end of external links
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.
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.
Re: regex to add target property to end of external links
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'):
For my help, I also add an class called "external", so I can format the links slightly differently:
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');
});
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');
});
Re: regex to add target property to end of external links
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.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.
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!
Re: regex to add target property to end of external links
Thanks so much! I just used Notepad++, any complete suggestion I was glad to go that route!!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
This worked brilliantly, much appreciated!
Re: regex to add target property to end of external links
I'm glad it worked. It's taken me forever to understand the more complex regex and I still count myself a hack beginner.