gulp-replace: Replace with inline regex variables not working

I’m trying to use this line: .pipe(replace(/^(.*)(\r?\n\1)+$/g, '$1')), And I’m not getting the expected result. On an example file of:

foo
foo
foo

It should return a file of foo. Thoughts?

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 19 (7 by maintainers)

Most upvoted comments

Thanks, I was able to clone that repo and reproduce the issue!

Figured out the problem I think. Sublime must be doing a multiline regex by default (?), or something; but that’s not the case in JS. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#boundaries will tell you that the ^ and & characters you were using, intending them to mean “start of line” and “end of line”, were actually meaning “start of input” and “end of input”.

In order go what you intend, you can either use the multiline flag m, or just remove the ^ and $ characters (since you’re already checking for line breaks, do you need them?).

Does that solve the issue for you @RichardLitt? It worked for me in your repo. (Either /^(.*)(\r?\n\1)+$/gm or /(.*)(\r?\n\1)+/g.)