Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's how to do it with <a href="http://jsoup.org" rel="nofollow">jSoup HTML parser</a>:</p> <pre><code>&lt;cfset jsoup = createObject('java','org.jsoup.Jsoup') /&gt; &lt;cfset Dom = jsoup.parse( InputHtml ) /&gt; &lt;cfset Dom.select('a[href]').attr('href','{replaced}') /&gt; &lt;cfset NewHtml = Dom.html() /&gt; </code></pre> <p>(On CF9 and earlier, this requires placing the jsoup's jar in CF's lib directory, or using <a href="http://javaloader.riaforge.org" rel="nofollow">JavaLoader</a>.)</p> <p>Using a HTML parser is usually better than using regex, not least because it's easier to maintain and understand.</p> <p>Here's an <em>imperfect</em> way of doing it with a regex:</p> <pre><code>&lt;cfset NewHtml = InputHtml.replaceAll ( '(?&lt;=&lt;a.{0,99}?\shref\s{0,99}?=\s{0,99}?)(?:"[^"]+|''[^'']+)(["'])' , '$1{replaced}$1' )/&gt; </code></pre> <p>Which hopefully demonstrates why using a tool such as jsoup is definitely the way to go...</p> <p><em>(btw, the above is using the Java regex engine (via <a href="http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#replaceAll" rel="nofollow">string.replaceAll</a>), so it can use the lookbehind functionality, which doesn't exist in CF's built-in regex (rereplace/rematch/etc))</em></p> <p><hr/> <strong>Update, based on the new code sample you've provided...</strong></p> <p>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:</p> <pre><code>&lt;cfset jsoup = createObject('java','org.jsoup.Jsoup') /&gt; &lt;cfset links = jsoup.parse( Arguments.HtmlCode ) &lt;!--- select all links beginning http and change their href ---&gt; .select('a[href^=http]').attr('href',' {clickurl}') &lt;!--- get HTML for all links, then split into array. ---&gt; .outerHtml().split('(?&lt;=&lt;/a&gt;)(?!$)') /&gt; &lt;cfdump var=#links# /&gt; </code></pre> <p>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.)</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload