Skip to content

Use expression character class instead of lookaround #581

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

Closed
RunDevelopment opened this issue Sep 8, 2023 · 0 comments · Fixed by #616
Closed

Use expression character class instead of lookaround #581

RunDevelopment opened this issue Sep 8, 2023 · 0 comments · Fixed by #616
Labels
enhancement New feature or request new rule

Comments

@RunDevelopment
Copy link
Collaborator

Motivation
Due to the lack of set operation for character classes, I wrote regexes like (?!\s)[\w\P{ASCII}] in the past (another example). They can be transformed to use set operations instead. Using set operation not only makes the intent clearer, it's also more performant. I did some performance test a while back, and (?!\s)[\w\P{ASCII}] was about 4x slower than [\w...whatever ranges are equivalent].

Description
Combine single-character lookarounds with character classes in regexes with the v flag. E.g. (?=[x])[y] => [y&&x] and (?![x])[y] => [y--x]. Regexes without the v should not be affected.

If the element after the lookaround is a character set, then the transformation should still be applied. E.g. (?!\s)\P{ASCII} => [\P{ASCII}--\s].

Examples

// good
/(?!\s)[\w\x80-\uFFFF]/;
/(?!\s)[\w\P{ASCII}]/u;

// bad
bad  = /(?!\s)[\w\P{ASCII}]/v;
good = /[[\w\P{ASCII}]--\s]/v;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request new rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant