简体   繁体   中英

regex to replace multiple subsequent line breaks

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');

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM