Gossamer Forum
Home : General : Perl Programming :

Need regexp help!

Quote Reply
Need regexp help!
Hi,

i'm trying to use a regexp for adding a "target=_blank" to all links like this:
Code:
$text =~ s/<a href="(*)">/<a href="$1" target="_blank">/g;

Anyone could help?

The target="_blank" should be inserted if it's missing ;)
Quote Reply
Re: [TB2] Need regexp help! In reply to
I'm not the best at regexes, but give this a go;

$text =~ s/<a href="(.+?)">/<a href="$1" target="_blank">/g;

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Need regexp help! In reply to
Hmm .. got it like this now:

Code:
$text =~ s/<a href="(.*?)">/<a href="$1" target="_blank">/g;

Any better ideas?
Quote Reply
Re: [TB2] Need regexp help! In reply to
Why are you using a * and not +, like in my code?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [TB2] Need regexp help! In reply to
Change:

(.*?)

to...

[^"]+
Quote Reply
Re: [Andy] Need regexp help! In reply to
In Reply To:
Why are you using a * and not +, like in my code?

Cheers


No idea .. i'm just playing with it .... any idea how to change the regexp cause all links are ending with a slash (like domain.com/") ?

Thanks for help!
Quote Reply
Re: [Andy] Need regexp help! In reply to
In Reply To:
I'm not the best at regexes, but give this a go;

$text =~ s/<a href="(.+?)">/<a href="$1" target="_blank">/g;

Cheers


Hi,

could anyone help me out with this regexp? It works so far but all my links are ending with a slash (e.g. www.domain.com/) ... how do i have to change this regexp in this case?
Quote Reply
Re: [TB2] Need regexp help! In reply to
Do the domains have a slash after the TLD (extension) normally, or does this seem to be added? Maybe you could give an example before/after the regex has been edited?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Need regexp help! In reply to
In Reply To:
Do the domains have a slash after the TLD (extension) normally, or does this seem to be added? Maybe you could give an example before/after the regex has been edited?

Cheers


Hi Andy,

in all of my values the domain is generally written like www.domain.com/ ... like

Code:
<a href="http://www.domain.com/">

and i need to change this to:

Code:
<a href="http://www.domain.com/" target="_blank">

Best would be, if the regexp doesn't care about the additional slash ... ;)

Thanks for your help!
Quote Reply
Re: [TB2] Need regexp help! In reply to
Why do you not want the slash?....it's far easier to let the regex match it than to leave it out.
Quote Reply
Re: [Paul] Need regexp help! In reply to
Basically it's equal for me, if the target="_blank" is added properly ;)

But my regexp-knowledge ends here ...
Quote Reply
Re: [TB2] Need regexp help! In reply to
I'm not sure what you mean. Is it not working?
Quote Reply
Re: [Paul] Need regexp help! In reply to
In Reply To:
I'm not sure what you mean. Is it not working?

Hmm .. basically i'm in the need of a regexp what adds target="_blank" to all links ... beside of the used syntax like:

Code:
<a href=http://www.domain.com>Link</a>

or

Code:
<a href='http://www.domain.com'>Link</a>

or

Code:
<a href="http://www.domain.com">Link</a>

or

Code:
<a href="http://www.domain.com/">Link</a>

or whatever ;)
Quote Reply
Re: [TB2] Need regexp help! In reply to
Instead of taking so much pain, I would have added a simple line in the HTML output <head> <base target="_blank"></head>

thanks,.