Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8427013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:41:01+00:00 2026-06-10T04:41:01+00:00

I need to replace the text inside all href values. I think a regular

  • 0

I need to replace the text inside all href values. I think a regular expression is the way to do it, but I’m no regex pro. Any thoughts on how I’d do the following using ColdFusion?

<a href="http://www.replace-this-link.com"></a>

so it is changed to:

<a href="{replaced}"></a>

Thanks!

Here’s an update to the question: I have this code and need the pattern below:

<cfset matches = ReMatch('<a[^>]*href="http[^"]*"[^>]*>(.+?)</a>', arguments.htmlCode) /> <cfdump var="#matches#">
<cfset links = arrayNew(1)>
<cfloop index="a" array="#matches#">
<cfset arrayAppend(links, rereplace(a, 'need regex'," {clickurl}","all"))>
</cfloop>
<cfdump var="#links#">
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T04:41:02+00:00Added an answer on June 10, 2026 at 4:41 am

    Here’s how to do it with jSoup HTML parser:

    <cfset jsoup = createObject('java','org.jsoup.Jsoup') />
    
    <cfset Dom = jsoup.parse( InputHtml ) />
    
    <cfset Dom.select('a[href]').attr('href','{replaced}') />
    
    <cfset NewHtml = Dom.html() />
    

    (On CF9 and earlier, this requires placing the jsoup’s jar in CF’s lib directory, or using JavaLoader.)

    Using a HTML parser is usually better than using regex, not least because it’s easier to maintain and understand.

    Here’s an imperfect way of doing it with a regex:

    <cfset NewHtml = InputHtml.replaceAll
        ( '(?<=<a.{0,99}?\shref\s{0,99}?=\s{0,99}?)(?:"[^"]+|''[^'']+)(["'])'
        , '$1{replaced}$1'
        )/>
    

    Which hopefully demonstrates why using a tool such as jsoup is definitely the way to go…

    (btw, the above is using the Java regex engine (via string.replaceAll), so it can use the lookbehind functionality, which doesn’t exist in CF’s built-in regex (rereplace/rematch/etc))


    Update, based on the new code sample you’ve provided…

    Here is an example of how to use jsoup for what you’re doing – it might still need some updates (depending on what {clickurl} is eventually going to be doing), but it currently functions the same as your sample code is attempting:

    <cfset jsoup = createObject('java','org.jsoup.Jsoup') />
    
    <cfset links = jsoup.parse( Arguments.HtmlCode )
    
        <!--- select all links beginning http and change their href --->
        .select('a[href^=http]').attr('href',' {clickurl}')
    
        <!--- get HTML for all links, then split into array. --->
        .outerHtml().split('(?<=</a>)(?!$)')
        />
    
    <cfdump var=#links# />
    

    That middle bit is all a single cfset, but I split it up and added comments for clarity. (You could of course do this with multiple variables and 3+ cfsets if you preferred that.)

    Again, it’s not a regex, because what you’re doing involves parsing HTML, and regex is not designed for parsing tag-based syntax, so isn’t very good at it – there are too many quirks and variations with HTML and describing them in a single regex gets very complicated very quickly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to to a RegEx search and replace of all commas found inside
I need to replace some value in given text using c# preferably using regex
I need to replace every instance of any text in square brackets with something
I need to convert lib_someString to <a href=someString>someString</a> inside a block of text using
I need help identifying twitter handles (I think it's called a handle..) inside plain-text
Possible Duplicate: Regular Expression to escape double quotes inside single quotes I need a
I need to replace some text in a JNLP file using a DOS batch
I've got a chunk of text in a StringBuilder object. I need to replace
Need to replace a domain name on all the links on the page that
I need to replace link index.php?a=2 ,but if it's have & after a=2 then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.