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

unicorn/better-regex autofix breaks "match all" in multiline regexps #895

Open
FloEdelmann opened this issue Oct 29, 2020 · 3 comments
Open
Labels

Comments

@FloEdelmann
Copy link
Contributor

FloEdelmann commented Oct 29, 2020

This regexp is autofixed by unicorn/better-regex like this:

- /lorem(?:.|\n)*?ipsum/m
+ /lorem[.\n]*?ipsum/m

This breaks the regex:

  • (.|\n) means "any character, including a newline character"
  • [.\n] means "a period character or a newline character"

Instead, it should be fixed to [^], according to https://stackoverflow.com/a/16119722/451391:

- /lorem(?:.|\n)*?ipsum/m
+ /lorem[^]*?ipsum/m
@RunDevelopment
Copy link

While this is definitely a bug, your proposed expected fix is incorrect. In JS RegExp, the dot (.) is equivalent to [^\r\n\u2028\u2029] (unless the s flag is enabled), so (?:.|\n) is equivalent to [^\r\u2028\u2029]. So the correct fix would be:

- /lorem(?:.|\n)*?ipsum/m
+ /lorem[^\r\u2028\u2029]*?ipsum/m

(Whether this fix is desirable is a different question.)

@sindresorhus
Copy link
Owner

sindresorhus commented Jan 12, 2021

This should be reported at https://github.com/DmitrySoshnikov/regexp-tree, which is what does the regex transformations.

If you can figure out which transform that causes this, it might be possible to disable that specific transform in the mean time:

optimized = optimize(original, undefined, {blacklist: ignoreList}).toString();

@FloEdelmann
Copy link
Contributor Author

I've opened an upstream issue: DmitrySoshnikov/regexp-tree#217

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

3 participants