gulp-replace icon indicating copy to clipboard operation
gulp-replace copied to clipboard

Remove empty lines

Open demisx opened this issue 9 years ago • 12 comments
trafficstars

Is it possible to remove empty lines in files using gulp-replace? I've tried this, but it didn't work:

  .pipe(replace(/^\s*$/, '', { skipBinary: true }))

Thanks.

demisx avatar Sep 21 '16 04:09 demisx

Hi @demisx, this is a regex question, try posting it on Stack Overflow and you're sure to get some answers!

lazd avatar Sep 21 '16 04:09 lazd

If you do find you're unable to get it to work after trying other strategies, please re-open the issue.

lazd avatar Sep 21 '16 04:09 lazd

Hi. Thank you for the quick response. I know that /^\s*$/ regex matches an empty line. It just doesn't work with gulp-replace for some reason. I'll try gulp-remove-empty-lines instead.

demisx avatar Sep 21 '16 04:09 demisx

@demisx Interesting, I will investigate it.

lazd avatar Sep 21 '16 04:09 lazd

@demisx that matches the whitespace itself, but it doesn't match the newline. Did you try /^\s*\n/ or something like that?

lazd avatar Sep 21 '16 04:09 lazd

No, I don't believe I've tried this one yet. And it did work. Sorry for the false alarm -- I need to refresh my memory on regex matches. Stupid me. Totally forgot that $ doesn't match \n.

demisx avatar Sep 21 '16 04:09 demisx

@demisx it happens to the best of us, I'm glad you solved it!

In the mean time, I am adding a test for this because it IS possible there could be weirdness between a buffered and streamed replace involving a newline, and I want to make sure it's working. Thanks!

lazd avatar Sep 21 '16 04:09 lazd

@lazd You are awesome man! I wish I could give you more than one star.

demisx avatar Sep 21 '16 04:09 demisx

@demisx so apparently /^\s*\n/gm is the right way to do it, or /^\s*[\r\n]/gm to also catch legacy Mac line endings. You need the m flag to match on a per-line basis, otherwise ^ matches the start of the string.

That said, the results are inconsistent between streams and buffers! This regex does not work when input is buffered, so this is still an issue!

lazd avatar Sep 21 '16 05:09 lazd

@demisx I pushed some tests to the blanklines branch, maybe you can have a look and see why the buffered test is failing? That's better than a hundred stars!

lazd avatar Sep 21 '16 05:09 lazd

tried it and it works.

replace(/(?:(?:\r\n|\r|\n)\s){2}/gm, '\r\n')

dashawk avatar Mar 30 '17 03:03 dashawk

Correct me if I'm wrong but the Sept 21st 2016 comment contains /^\s*[\r\n]/gm which might be incorrect. Brackets means "one of these characters". If there is \r\n this would catch \r and not \n.

If you want to check in regexr, try: \n[ab] with gm flags and following copy:


ab


regex

@dashawk snippet is better, it uses "or" but it only removes more than 2 consecutive empty lines. I'm not sure that's exactly what was asked — cases of more than 1 consecutive empty line should be replaced with a single line break.


What if we simply replaced instances of:

/(\r\n|\r|\n){2,}/gm

with \n? The {2,} bit means "two or more".

revelt avatar May 17 '18 15:05 revelt