1

Here is what I want to do.

  1. Replace more than 3 line breaks with 3 line breaks.
  2. Replace 2 line breaks with 1 line break.
  3. Ignore single line breaks.

So that something like this:

Dear Bob



I would love to not have so many line breaks here.

This is a new paragraph.




Thanks

Jim

Ends up more like this:

Dear Bob

I would love to not have so many line breaks here.
This is a new paragraph.

Thanks
Jim

Based on another question, this is the closest I've come, but it's not quite right:

innerHTML.replace(/\n\n\s*\n\n/g, '\n');
7
  • Not quite right how? Remember "more than three" means \n\n\n+. Commented Mar 29, 2016 at 21:30
  • It's hard to explain because the real data is private. I will try to obfuscate it and add the exact results to my question as soon as I can. Commented Mar 29, 2016 at 21:31
  • Will there be \n only line breaks? No \r\ns? Commented Mar 29, 2016 at 21:32
  • I'm not sure, that may be part of the problem. Commented Mar 29, 2016 at 21:33
  • If you can create some dummy text in a similar structure that would be sufficient. Commented Mar 29, 2016 at 21:37

1 Answer 1

1

You may use a regex with an alternation group, one alternative will match 4+ linebreaks, and the other just 2 (that is not preceded nor followed with a line break).

The regex will be:

((?:\r?\n){4,})|(^|[^\n])(?:\r?\n){2}(?!\r?\n)

Explanation:

  • ((?:\r?\n){4,}) - Alternative 1 matching 4+ sequences of an optional \r followed with a compulsory \n
  • | - or...
  • (^|[^\n])(?:\r?\n){2}(?!\r?\n) - Alternative 2 matching exactly 2 sequences of an optional \r followed with a compulsory \n that are not preceded ((^|[^\n]) matches either the start of string or a character other than \n) nor followed with a linebreak (the negative lookahead (?!\r?\n) makes sure of that).

In the replacement, there is a callback checking which alternative matched and replaces accodingly.

The JS code demo is below:

var re = /((?:\r?\n){4,})|(^|[^\n])(?:\r?\n){2}(?!\r?\n)/g; 
var str = `Dear Bob



I would love to not have so many line breaks here.

This is a new paragraph.




Thanks

Jim`;
var result = str.replace(re, function (m, g1, g2) {
	return g1 ? "\n\n" : g2 + "\n";
});
document.body.innerHTML = "<pre>" + result + "</pre>";

Sign up to request clarification or add additional context in comments.

3 Comments

You can adjust the linebreaks, e.g. add \r.
If this were Reddit I would tag you as Regex God
I am not a regex god. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.