Incorrect advisory about removing global flag with replaceAll using a RegEx

  • What language is this for?
    • JavaScript
  • Which rule?
    • typescript:S7781
  • Why do you believe it’s a false-positive/false-negative?
    • it’s not 100% false-positive, but it’s slightly misleading as the description in the advisory states that the global flag can be removed. Which is only true if the first argument is NOT a RegEx. Otherwise the flag needs to be present to avoid a TypeError.
  • Are you using
    • SonarQube Cloud?
      • No
    • SonarQube Server / Community Build - which version?
      • Enterprise Edition v2025.5 (113872)

Advisory says

This rule flags when String#replace() is used with a global regex pattern, or when String#replaceAll() is used with a global regex flag.

Additionally, when String#replaceAll() is used with a regex, the global flag is redundant since the method inherently replaces all occurrences.

MDN says

If pattern is a regex, then it must have the global (g) flag set, or a TypeError is thrown.

This is also clear from looking at the linked eslint plugin rule examples.

Hello @pzi,

thanks for the feedback. We are aware the rule description is wrong and we fixed it some days ago. Next release will contain the description with the fix you mentioned.

Cheers

1 Like