Related Articles

6 users responded in this post

Subscribe to this post comment rss or trackback url
User Gravatar
Matthew said in February 3rd, 2010 at 6:01 pm

Hi Jake

Why not just use the ColdFusion ReReplace() function like below?

Cheers
Matthew

User Gravatar
Matthew said in February 3rd, 2010 at 6:02 pm

I’ll try again:

>cfset contentVariable = ReReplace(contentVariable,’src=”(\/.+?)\”‘, ‘src=”#domainName#\1″‘,’all’) /<

User Gravatar
Peter Boughton said in February 3rd, 2010 at 6:20 pm

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.

User Gravatar
Peter Boughton said in February 3rd, 2010 at 6:23 pm

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.)

User Gravatar
Ben Nadel said in February 4th, 2010 at 2:15 pm

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.

User Gravatar
Jake Churchill said in February 8th, 2010 at 4:57 pm

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

 Username (Required)

 Email Address (Remains Private)

 Website (Optional)