ota-meshi / eslint-plugin-regexp

ESLint plugin for finding regex mistakes and style guide violations.

Home Page:https://ota-meshi.github.io/eslint-plugin-regexp/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`prefer-lookaround` Not checking groups contains negate CharacterClass

fisker opened this issue · comments

Information:

  • ESLint version: 8.27.0
  • eslint-plugin-regexp version: 7.31.10

Description

'ab'.replaceAll(/a([b])/g, "$1");
'ac'.replaceAll(/a([^b])/g, "$1");

https://ota-meshi.github.io/eslint-plugin-regexp/playground/#eJyrVkrOT0lVslJST0xS1ytKLchJTE51zMnR0E/UiE6K1dRP11GIUVIxjFHStI7JU09MxlAUh6ZKSUepqDQntVjJqlqpKDU9taJAv6AoNS21SDcnPz87sSi/NC9FycqothYA9c4mgQ==

The first is reported.

The second is not, it should be able to fix to

'ac'.replaceAll(/a(?=[^b])/g, "");

Thank you for posting issue.

But, it's an intentional behavior.
Replacing it with lookahead has the side effect of not consuming any characters.

'aaaaa'.replaceAll(/a([^b])/g, ",$1");
// ',a,aa'
'aaaaa'.replaceAll(/a(?=[^b])/g, ",");
// ',,,,a'

The following negated character classes are reported because they have no side effects when replaced with lookahead.

'ac'.replaceAll(/a([^a])/g, "$1");

Thanks for the information.