Pete Freitag Pete Freitag

Regex to Replace Multiple Blank Lines with One

coldfusionweb

I'm working on a function to strip HTML, but preserve things like paragraph spacing. In removing HTML tags, sometimes you end up with lots of blank lines... Here's a quick regular expression to convert multiple blank lines with just one \n character:

<cfset content = ReReplace(content, "[\r\n]+", "#Chr(10)#", "ALL")>

Like this? Follow me ↯

Regex to Replace Multiple Blank Lines with One was first published on May 19, 2005.

If you like reading about regex, cfml, rereplace, or html then you might also like:

FuseGuard Web App Firewall for ColdFusion

The FuseGuard Web Application Firewall for ColdFusion & CFML is a high performance, customizable engine that blocks various attacks against your ColdFusion applications.

Comments

Will for that, try something like this as the pattern:

[\r\n]+\t*[\r\n]*
by Pete Freitag on 05/19/2005 at 1:54:56 PM UTC
That will only match one line with tabs at a time. so \n\t\n\t\n\t will be converted to \n\t\n, not just \n. You'll need [\r\n][\r\n\t]*[\r\n] or something like it.
by Barney on 05/19/2005 at 2:57:32 PM UTC
By the way, I have a handy tool that you can use to play with Regex: http://www.petefreitag.com/tools/find_replace/
by Pete Freitag on 05/19/2005 at 3:02:20 PM UTC
that's great! been looking for a function/cfc that does a good job of stripping out the HTML from form posts (before they get INSERT'd into the db)... preferably one with a list of optional "okay" HTML tags that can be passed in...

never been good with RegEx, though.
by forgetfoo on 05/20/2005 at 12:45:12 PM UTC
Domain for sale quality results found here
by accountingapp.net on 06/29/2006 at 11:56:54 AM UTC