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.

New Rule: require global regular expression to be used with `replaceAll`

See original GitHub issue

Rule details

When replaceAll is given a regular expression as its first argument, that regular expression must be global. If it’s not global, a runtime error is thrown. This rule should require that regular expressions passed as the first argument to replaceAll are global.

Related ECMAScript feature

String.prototype.replaceAll: https://github.com/tc39/proposal-string-replaceall

What type of rule is this?

Warns about a potential problem

Example code

`abc`.replaceAll(/a/, ``) // throws Uncaught TypeError: String.prototype.replaceAll called with a non-global RegExp argument

// Should be an error, and could also have an auto-fix to change the code to `abc`.replaceAll(/a/g, ``)

Why should this rule be in the core instead of a plugin?

It prevents a runtime error that’s not otherwise apparent.

Participation

  • I am willing to submit a pull request to implement this rule.

Additional comments

It’d be nice to reopen https://github.com/eslint/eslint/issues/12557, too.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Tyler-Murphycommented, Sep 19, 2021

@bmish, I think that’s different. Maybe for https://github.com/eslint/eslint/issues/12557?

1reaction
mdjermanoviccommented, Sep 16, 2021

It sounds like no-restricted-syntax might be able to cover the case I’m thinking of. Do you think it could?

This selector would cover regex literals:

CallExpression[callee.property.name='replaceAll'] > Literal[regex.flags=/^[^g]*$/].arguments:first-child

Online Demo

Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript String replace vs replaceAll - Stack Overflow
In other words, when calling replaceAll with a regex literal or RegExp , it must use the global flag. So, there doesn't seem...
Read more >
String.prototype.replaceAll() - JavaScript - MDN Web Docs
The replaceAll() method returns a new string with all matches of a pattern ... global flag required when calling replaceAll with regex.
Read more >
String.prototype.replaceAll() should not be called ... - DeepScan
This rule applies when a regular expression without the global flag ( g ) is used at String.prototype.replaceAll() .
Read more >
43 Regular expressions ( RegExp ) - Exploring JS
A new regular expression is created as specified via pattern . If flags is missing, the empty string '' is used. ... Rules...
Read more >
Node.js — String Replace All Appearances - Future Studio
As you can see, using a string as the substring to search for will only replace the first appearance. A regular expression with...
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