Forbid `g` flag used on `RegExp#test()`
See original GitHub issueGot the idea from https://github.com/sindresorhus/eslint-plugin-unicorn/pull/1173
If the regex is only used once on RegExp#test()
, the g
flag is useless (~maybe also forbid y
flag?~)
Fail
/foo/g.test(bar);
new RegExp('foo', 'g').test(bar);
Pass
const regex = /foo/g;
regex.test(bar);
I didn’t mark this issue as rule proposal, because I was thinking maybe we can add this to better-regex
?
Issue Analytics
- State:
- Created 2 years ago
- Reactions:4
- Comments:5 (2 by maintainers)
Top Results From Across the Web
What is the meaning of the 'g' flag in regular expressions?
The "g" flag indicates that the regular expression should be tested against all possible matches in a string. Without the g flag, it'll...
Read more >JavaScript regular expressions: `/g`, `/y`, and `.lastIndex` - 2ality
In this blog post, we examine how the RegExp flags /g and /y work and how they depend on the RegExp property .lastIndex....
Read more >toMatch behaves oddly for regex with global flag #9283 - GitHub
We're upgrading from Jest 23 to 24 and encountered an odd breaking test. When testing a RegExp with the g flag, the regular...
Read more >43 Regular expressions ( RegExp ) - Exploring JS
String.prototype.matchAll() returns an iterable of match objects (flag /g must be set; otherwise, an exception is thrown).
Read more >Regular expressions with the global flag should be used with ...
This rule raises an issue when: a regular expression is tested against different inputs with RegExp.prototype.test() or RegExp.prototype.exec(); a ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Another possible problem in regex used to test. It should not capture group.
Fail
Pass
Update: This rule seems doing a better job.
I was wrong about
y
flag