Proposal: disallow regular expressions with the global match flag `g` at the module level
See original GitHub issuePlease describe what the rule should do:
Regular expressions with the g
flag maintain state, which can cause confusion and subtle bugs if the regex is defined at the module level. It would be nice if there was an eslint rule to prevent this.
It might also be worth flagging regexes with the g
flag in the global scope.
What category of rule is this? (place an “X” next to just one item)
[ ] Enforces code style [x] Warns about a potential error [ ] Suggests an alternate way of doing something [ ] Other (please specify:)
Provide 2-3 code examples that this rule will warn about:
Bad:
const foo = /./g;
const foo = /./gi;
const bar = new RegExp('.', 'g');
const baz = new RegExp(/./, 'g');
Okay:
function() {
const foo = /./g;
}
function() {
const bar = new RegExp('.', 'g');
}
const foo = /./;
const bar = new RegExp('.');
Why should this rule be included in ESLint (instead of a plugin)?
This is a source of bugs in a core part of the language.
cc @ljharb
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:9 (9 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 >Regular expressions - JavaScript - MDN Web Docs
The "g" after the regular expression is an option or flag that performs a global search, looking in the whole string and returning...
Read more >Regular Expressions: Regexes in Python (Part 2)
The function returns a match object if it finds a match and None otherwise. re.match(<regex>, <string>, flags=0). Looks for a regex match at...
Read more >Issue 2636: Adding a new regex module (compatible with re)
msg65513 ‑ (view) Author: Jeffrey C. Jacobs (timehorse) Date: 2008‑04‑15 11:57
msg65593 ‑ (view) Author: Jeffrey C. Jacobs (timehorse) Date: 2008‑04‑17 22:06
msg65613 ‑ (view)...
Read more >Python re Module - Regex Support - Regular-Expressions.info
To get all matches from a string, call re. findall(regex, subject). This will return an array of all non-overlapping regex matches in the...
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
Definitely agree. I’m just concerned about whether the “common case” for this rule (accidental mutation) for this rule is actually much more common than the safe case. Sure, people can use override comments for exceptional cases, but if they need to be used for a very significant percentage of errors reported by a rule, then the rule can be sort of irritating to use.
This bit me specifically because I thought about
lastIndex
, and intentionally used theg
flag, thinking it would protect me from the mutation - when in fact, it was the reverse.Essentially this rule should warn whenever a regex literal (or
new RegExp
) with the global flagg
(or the sticky flagy
, which also suffers from this issue) is re-used across scopes.