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: / 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.
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/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.
Share Improve this question edited May 23, 2017 at 12:08 CommunityBot 11 silver badge asked Feb 16, 2014 at 22:15 FijjitFijjit 1,4972 gold badges19 silver badges33 bronze badges 4- What happens when you have 3 blank lines, that upon replacing the double blank, bees another double blank, and your regex has moved on and is satisfied? – user557597 Commented Feb 17, 2014 at 0:26
- Have you considered that one/some blank lines could be full of non-newline whitespace, and what should happen to those lines? – user557597 Commented Feb 17, 2014 at 0: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 Commented Feb 18, 2014 at 13:04
- Try this regex: /^\s*[\r\n]/gm – Rumplin Commented Sep 13, 2016 at 11:03
4 Answers
Reset to default 6This is an old question, but I don't care, it es 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.
Just use it like this:
var inputString = '...';
var outputString = inputString.replace(/^(\s*\r\n){2,}/, "\r\n")
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:
Why other answers are wrong? Check this out:
See \n for all "new line"
Replaced some, but not all
Replaced some, but not all again
Using simple regular expressions:
str = "whatever your string is";
reg = /\n\n\n/;
str.replace(reg, "\n\n");