lazd / gulp-replace

A string replace plugin for gulp
MIT License
496 stars 89 forks source link

Remove empty lines #74

Open demisx opened 8 years ago

demisx commented 8 years ago

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.

lazd commented 8 years ago

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

lazd commented 8 years ago

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

demisx commented 8 years ago

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.

lazd commented 8 years ago

@demisx Interesting, I will investigate it.

lazd commented 8 years ago

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

demisx commented 8 years ago

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.

lazd commented 8 years ago

@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!

demisx commented 8 years ago

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

lazd commented 8 years ago

@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 commented 8 years ago

@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!

dashawk commented 7 years ago

tried it and it works.

replace(/(?:(?:\r\n|\r|\n)\s){2}/gm, '\r\n')
revelt commented 6 years ago

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