question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Forbid `g` flag used on `RegExp#test()`

See original GitHub issue

Got 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:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
fiskercommented, Apr 9, 2021

Another possible problem in regex used to test. It should not capture group.

Fail

/(foo)/.test('foo')
/(?<named>foo)/.test('foo')

Pass

/([-_])foo\1/.test('_foo-')
/(?<separator>[-_])foo\k<separator>/.test('_foo-')

Update: This rule seems doing a better job.

1reaction
fiskercommented, Apr 9, 2021

I was wrong about y flag

/foo/.test('-foo');
true
/foo/y.test('-foo');
false
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found