javascript.lang.correctness.no-replaceall.no-replaceall

profile photo of semgrepsemgrep
Author
3,526
Download Count*

The string method replaceAll is not supported in all versions of javascript, and is not supported by older browser versions. Consider using replace() with a regex as the first argument instead like mystring.replace(/bad/g, "good") instead of mystring.replaceAll("bad", "good") (https://discourse.threejs.org/t/replaceall-is-not-a-function/14585)

Run Locally

Run in CI

Defintion

rules:
  - id: no-replaceall
    message: The string method replaceAll is not supported in all versions of
      javascript, and is not supported by older browser versions. Consider using
      replace() with a regex as the first argument instead like
      mystring.replace(/bad/g, "good") instead of mystring.replaceAll("bad",
      "good")
      (https://discourse.threejs.org/t/replaceall-is-not-a-function/14585)
    severity: WARNING
    languages:
      - javascript
      - typescript
    pattern: $STRING.replaceAll("...",$NEW)
    metadata:
      category: correctness
      technology:
        - javascript
      references:
        - https://discourse.threejs.org/t/replaceall-is-not-a-function/14585
      license: Commons Clause License Condition v1.0[LGPL-2.1-only]

Examples

no-replaceall.js

const baba = "baba"
// ruleid:no-replaceall
const str1 = old_str1.replaceAll(baba, "    ");
// ok:no-replaceall
const str1 = old_str1.replaceAll(hello, "    ");
// ruleid:no-replaceall
const str2 = old_str2.replaceAll("\t", "    ")
// ok:no-replaceall
const str3 = old_str3.replace("\t", "    ");