Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
Ben Nadel at cf.Objective() 2010 (Minneapolis, MN) with: Doug Hughes and Ezra Parker and Dan Wilson and John Mason and Jason Dean and Luis Majano and Mark Mandel and Brian Kotek and Wil Genovese and Rob Brooks-Bilson and Andy Matthews and Simeon Bateman and Ray Camden and Chris Rockett and Joe Bernard and Dan Skaggs and Byron Raines and Barney Boisvert and Simon Free and Steve 'Cutter' Blades and Seth Bienek and Katie Bienek and Jeff Coughlin
Ben Nadel at cf.Objective() 2010 (Minneapolis, MN) with: Doug Hughes@doughughes ) , Ezra Parker , Dan Wilson@DanWilson ) , John Mason@john_mason_ ) , Jason Dean@JasonPDean ) , Luis Majano@lmajano ) , Mark Mandel@Neurotic ) , Brian Kotek , Wil Genovese@wilgeno ) , Rob Brooks-Bilson@styggiti ) , Andy Matthews@commadelimited ) , Simeon Bateman@simBateman ) , Ray Camden@cfjedimaster ) , Chris Rockett ( @RockettMan ) , Joe Bernard@JEBernard ) , Dan Skaggs@dskaggs ) , Byron Raines ( @byronraines ) , Barney Boisvert@barneyb ) , Simon Free@simonfree ) , Steve 'Cutter' Blades@cutterbl ) , Seth Bienek@sethbienek ) , Katie Bienek@KatieBienek ) , and Jeff Coughlin@jeffcoughlin )

ColdFusion Regular Expressions Match New Line Using "."

By Ben Nadel on
Tags: ColdFusion

Minor note here, but over on CF-Talk, Rick Root just pointed out a flaw in my understanding of ColdFusion regular expressions. I was under the impression that the period "." would NOT match the new line character or the carriage return character in a target string. I think this is traditionally how regular expressions work (or maybe not). It turns out, that this is dead wrong. The "." character DOES match new lines and carriage returns. I am not sure if this changed in MX(?7) or if I just never learned in properly.

Thanks Rick!

My test:

  • <cfsavecontent variable="strText">
  • <a href=""
  • target="_blank"
  • >Google</a>
  • </cfsavecontent>
  • <!--- Replace tags. --->
  • #REReplaceNoCase( strText, "</?[a-z].*?>", "", "ALL" )#

Looking For A New Job?

Ooops, there are no jobs. Post one now for only $29 and own this real estate!

100% of job board revenue is donated to Kiva. Loans that change livesFind out more »

Reader Comments

Yep, this has always been the case. From "Ben Forta's Advanced CFMX Application Development" (3rd ed.);

"It's important to understand that in ColdFusion MX, the dot character always matches newlines, which is not always the case with Perl."

Good to know this (despite the fact it says it clearly in the docs, right back to CF5... who RTFMs? ;-)

I just ass-u-me`d that CF followed the practice set by PERL and Java. No. That would be too sensible.

Cheers for the heads-up.


I think the problem is that I read the text book and learned a lot about ColdFusion before I even learned how to use Regular Expressions. So, by the time I knew what they were and how to apply them, I was no longer learning from the documentation... I was learning from google'd examples of regular expressions.

Other's have said this, but Perl does it the way you stated, Ben. From the Perl docs: ". matches any single character except a newline". And ColdFusion regular expressions are based on the Perl syntax, but apparently they took liberties. :)

Boyan, excellent point. Just a follow up, that would only work if you don't mind losing other line breaks (outside of tags).

It hurts my feelings tremendously that ColdFusion does this. I hate using [^\r\n] for staying within lines. And I typically use [\S\s] to match any character if the regex library I'm using doesn't support an explicit "dot matches newline" option. Semi-related sidenote: Non-negated \r\n pairs are unreliable in JavaScript, since Firefox uses only linefeeds (\n) to separate lines in <textarea>s. Instead, I typically use \r?\n, which works cross-browser.

Yeah, I have run into that \r\n thing in Javascript before. It was driving me CRAZY! Cause I was outputting the ASCII values in ColdFusion to debug and sure enough it was there, but then in Javascript when I was testing for it, NOT there. That was like 30 minutes of my life I won't get back :)