1

Hopefully a simple question.

If I have text like this:

Orange
Apples


Melons

Bananas

...how can I replace all occurrences of multiple blank lines with single blank lines, ending up with this:

Orange
Apples

Melons

Bananas

Fiddle: http://jsfiddle.net/Jq4pT/ Need to remove multiple blank lines from the first box's input before inserting into the second.

I've found this link, but am not sure how to use that regular expression in Javascript?

Thanks.

Community
  • 1
  • 1
Fijjit
  • 1,399
  • 2
  • 17
  • 31
  • What happens when you have 3 blank lines, that upon replacing the double blank, becomes another double blank, and your regex has moved on and is satisfied? –  Feb 17 '14 at 00:26
  • Have you considered that one/some blank lines could be full of non-newline whitespace, and what should happen to those lines? –  Feb 17 '14 at 00:37
  • Edited to make it clear that the aim is to remove multiple blank lines, not necessarily just two. Basically, if there's more than one blank line in a row, reduce down to just one. – Fijjit Feb 18 '14 at 13:04
  • Try this regex: /^\s*[\r\n]/gm – Rumplin Sep 13 '16 at 11:03

4 Answers4

5

This is an old question, but I don't care, it comes up first in Google search.

You can do this with the following code:

var EOL = string.match(/\r\n/gm)?"\r\n":"\n";
var regExp = new RegExp("("+EOL+"){3,}", "gm");
string = string.replace(regExp, EOL+EOL);
  • First it determines the endline char to use in Regex
  • Regex looks for EOL chars being repeated 3 or more times, and replaces them with 2 EOL chars.
  • Using g modifier to replace all occurrences
  • Using m modifier to apply regex to the whole multi-line string, instead of per line.
Cipi
  • 11,055
  • 9
  • 47
  • 60
2

Just use it like this:

var inputString = '...';
var outputString = inputString.replace(/^(\s*\r\n){2,}/, "\r\n")
Maksim Gladkov
  • 3,051
  • 1
  • 14
  • 16
  • Wont this replace 2 or more (alot more) with a single line-break? –  Feb 17 '14 at 00:29
  • Hi - couldn't get this to work in my case. I've added a fiddle to illustrate the problem better: http://jsfiddle.net/Jq4pT/ Need to get input from first textarea and remove multiple blank lines before adding to the second. Would you mind taking a look? – Fijjit Feb 18 '14 at 13:20
1

Other one-liner answers in this thread are wrong while those won't replace all new lines sequences with 2 new lines but will do that only once. Those will run once over the string. Which means if there will be 6 new lines, i twill replace 3 to 2 and next 3 to 2 again. So result would be 4 new lines. Then you would need to run this code again, which would replace 3 new lines with 2 and leftover of 1 new line would be untouched, so after 2nd run you would have 3 new lines. If you would run for the 3rd time only then you would reduce from 6 new lines to just 2.

So the correct one-liner answer is this:

yourString.replace(/^\s*[\r\n]|^\s+| +(?= )| +$|\s+$(?![^])/gm, '\n\n');

Full sample:

const a = `





a




b





`;

const result = a.replace(/^\s*[\r\n]|^\s+| +(?= )| +$|\s+$(?![^])/gm, '\n\n');

console.log(result);

Result will be as you would expect:

enter image description here


Why other answers are wrong? Check this out:

enter image description here

See \n for all "new line"

enter image description here

Replaced some, but not all

enter image description here

Replaced some, but not all again

Lukas Liesis
  • 24,652
  • 10
  • 111
  • 109
0

Using simple regular expressions:

str = "whatever your string is";
reg = /\n\n\n/;
str.replace(reg, "\n\n");
mcwayliffe
  • 386
  • 2
  • 10