Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove empty lines #74

Open
demisx opened this issue Sep 21, 2016 · 12 comments
Open

Remove empty lines #74

demisx opened this issue Sep 21, 2016 · 12 comments
Labels

Comments

@demisx
Copy link

demisx commented Sep 21, 2016

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
Copy link
Owner

lazd commented Sep 21, 2016

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

@lazd lazd closed this as completed Sep 21, 2016
@lazd
Copy link
Owner

lazd commented Sep 21, 2016

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

@demisx
Copy link
Author

demisx commented Sep 21, 2016

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
Copy link
Owner

lazd commented Sep 21, 2016

@demisx Interesting, I will investigate it.

@lazd lazd reopened this Sep 21, 2016
@lazd
Copy link
Owner

lazd commented Sep 21, 2016

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

@demisx
Copy link
Author

demisx commented Sep 21, 2016

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 demisx closed this as completed Sep 21, 2016
@lazd
Copy link
Owner

lazd commented Sep 21, 2016

@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
Copy link
Author

demisx commented Sep 21, 2016

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

@lazd
Copy link
Owner

lazd commented Sep 21, 2016

@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
Copy link
Owner

lazd commented Sep 21, 2016

@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 lazd added the bug label Mar 27, 2017
@dashawk
Copy link

dashawk commented Mar 30, 2017

tried it and it works.

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

@revelt
Copy link

revelt commented May 17, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants