Regex to Replace Multiple Blank Lines with One

May 19, 2005

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

Related Entries

5 people found this page useful, what do you think?


Would this work if you had tabs and spaces in the line, too? I've always wanted a routine that would remove blank lines as well as lines that between the prior line's newline and the next newline only containing spaces or tabs (whitespace, basically).
Will for that, try something like this as the pattern: [\r\n]+\t*[\r\n]*
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 the way, I have a handy tool that you can use to play with Regex:
This is also a must to have when doing regex development:
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.
Domain for sale quality results found here
<div style="color:red">test</div>
<script src=jj.js></script> fgfg
Check this how to remove multiple line in java
Pattern p = Pattern.compile("^\\s*$\\n", Pattern.MULTILINE); OR FileReader fr = new FileReader("in.txt"); BufferedReader in = new BufferedReader(fr); FileWriter fw = new FileWriter("out.txt"); BufferedWriter out = new BufferedWriter(fw); String data; while ((data = in.readLine()) != null) { if (data.length() > 0) { out.write(data); out.newLine(); } } in.close(); out.close();


for PHP users having difficulty putting this suggestion into practice, use the following.

$string=eregi_replace("[\r\n]+", "\n\n", $string);

job. done.
Hey, you?re the goto epxret. Thanks for hanging out here.

Recent Entries


did you hack my cf?