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#">
Here’s how to do it with jSoup HTML parser:
(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:
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:
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.