Open demisx opened 8 years ago
Hi @demisx, this is a regex question, try posting it on Stack Overflow and you're sure to get some answers!
If you do find you're unable to get it to work after trying other strategies, please re-open the issue.
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 Interesting, I will investigate it.
@demisx that matches the whitespace itself, but it doesn't match the newline. Did you try /^\s*\n/
or something like that?
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 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 You are awesome man! I wish I could give you more than one star.
@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!
@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!
tried it and it works.
replace(/(?:(?:\r\n|\r|\n)\s){2}/gm, '\r\n')
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
@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".
Is it possible to remove empty lines in files using
gulp-replace
? I've tried this, but it didn't work:Thanks.