Add Target Attribute to a Link


I am trying to add the target attribute to links in an rss feed.

Here is my code:

<cfset rssContent = REReplace(rssContent,'<a href="http://[^"]+">','<a href="http://[^"]+" target="_blank">','ALL')>

It is adding the target attribute but it's destroying the link information.

Who is Participating?
käµfm³d 👽Connect With a Mentor Commented:
Try this:
<cfset rssContent = REReplace(rssContent,'(<a href="http://[^"]+")>','\1 target="_blank">','ALL')>

Open in new window

or you can simply replace the closing >

<cfset out = replacenocase(rssContent, ">",  " target=""_blank"">","ALL")>

btw - in either case you should use the replaceNoCase or reReplaceNoCase if you don't have control of the source of the input as replace is case sensitive it will miss vs

RayBakkerAuthor Commented:
Works perfectly. Thank you.
käµfm³d 👽Commented:
NP. Glad to help  :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.