Yesterday, I found myself needing more than ColdFusion could supply. I had a variable with content that came from a WYSIWYG editor. Some of that content was the result of inserting images into the content. By default the image source was set to the absolute path from the webroot (i.e. /images/imagefile.jpg). This worked great for the web, but I needed the same content to be sent via email which did not work so well.
I needed to find all instances of the “src” attribute and tack on a domain so the ending result would be http://domain.com/images/imagefile.jpg and therefore would be email friendly. I tried several ways of doing this (combinations of REFind, REMatch, etc) but everything ended up being more difficult than it should have been.
Luckily, a ColdFusion String maps directly to a Java String so even though the docs might not suggest it, there are methods available for use. The one I wanted was “replaceAll()”. It does a simple regex replace and allows for backreferencing ($1, $2, etc). Backreferencing is key to not lose content that I needed (i.e. /images/imagefile.jpg). So, here’s the code that finally made my day:
<cfset domainName = "http://domain.com" />
<cfset contentVariable = contentVariable.replaceAll("src=\""(\/.+?)\""", "src=\""#domainName#$1\""") />
Full documentation on Java’s String functions can be seen at the Java Docs -> String

Related Articles
6 users responded in this post
Hi Jake
Why not just use the ColdFusion ReReplace() function like below?
Cheers
Matthew
I’ll try again:
>cfset contentVariable = ReReplace(contentVariable,’src=”(\/.+?)\”‘, ‘src=”#domainName#\1″‘,’all’) /<
I’m confused – whilst its useful for people to know they can use Java String functions – you’re not using anything that requires Java regex here – you can do everything you did with regular CF!
Here’s an example that uses Java’s lookbehind and possessive quantifiers, for something that is (potentially) very slightly better performance, like so:
<cfset contentVariable = contentVariable.replaceAll( ‘(?
But this is still using Regex to parse HTML, which has a number of pitfalls. The ideal method is using a DOM parser instead.
Hmmm. The tags have been broken in that post – I’ll repost with them replaced by {braces}
—
I’m confused – you’re not using anything Java regex here – you can do everything here with CF!
{cfset contentVariable = rereplace( contentVariable , ‘src=”(/.+?)”‘ , ‘src=”#domainName#\1″‘ ) /}
Though you could of course have used Java’s lookbehind and possessive quantifiers, for something that is (potentially) very slightly better performance, like so:
{cfset contentVariable = contentVariable.replaceAll( ‘(?<=src=”)/[^"]++’ , ‘#domainName#$0′ )/}
But this is still using Regex to parse HTML, which has a number of pitfalls. The ideal method is using a DOM parser instead.
The ideal way is to use a DOM parser instead. (The best we’ve got for CF is XmlParse, which isn’t great; there are Java DOM parsers which I keep meaning to check out but haven’t got around to yet.)
The Java regular expression engine (that you are accessing) is definitely awesome. It’s faster and more robust that the one exposed by ColdFusion (POSIX). That said, ColdFusion’s reReplace() should be able to handle this as well; you just have to be careful because the back-references use “\N” rather than “$N” notation.
Thanks for the comments. The backreferencing is the exact reason I dropped back to Java for this. I did not know to use a “\1″ instead of a “$1″.
Leave A Reply