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.

Proposal: disallow regular expressions with the global match flag `g` at the module level

See original GitHub issue

Please 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:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
not-an-aardvarkcommented, Feb 27, 2017

The point of a rule isn’t necessarily to cover all cases, it’s often to do the safe/best thing in the common case.

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.

1reaction
ljharbcommented, Feb 16, 2017

This bit me specifically because I thought about lastIndex, and intentionally used the g 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 flag g (or the sticky flag y, which also suffers from this issue) is re-used across scopes.

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

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