More than once in the past I've wondered about the problem of formatting blocks of text so that all runs of whitespace are 'collapsed' into a single space, except that paragraphs should be conserved - meaning that all runs of blank lines are collapsed into single blank lines, but not collapsed into just a space.
A blank line is of course two end-of-line characters (typically carriage return or linefeed or both) without any intervening non-whitespace characters. (There may be other whitespace though such as spaces or tabs).
This is surely a pretty common problem and though not difficult to solve I'm always dissatisfied with my solutions, which lack elegance or leave open loopholes. Surely there is an elegant expressive way to do this.
I'm leaving this open to all regex flavours since I've wanted to do it in at least Perl, Vim, and JavaScript. Here's my most recent lazy attempt to do it in node.js, the loophole is obviously the magic word. It's probably pretty typical of the unsatisfactory solutions I've used::
text = text.replace(/\r?\n(?:\s*\r?\n)+/g, '_SomeMagicWord_');
text = text.replace(/\s\s+/gm, ' ');
text = text.replace(/_SomeMagicWord_/g, '\r\n\r\n');
In case my explanation is not clear it should transform from this:
foo bar baz
fred barney wilma
one two three
to this:
foo bar baz fred barney wilma
one two three
(Watch out for trailing whitespace at the ends of lines too!)