Found non-literal argument to RegExp Constructor - Eslint issue

Viewed 3509
const str = 'Test test1 test2'
const pat = 'test';
const re = new RegExp(pat, 'i'); //Found non-literal argument to RegExp Constructoreslint(security/detect-non-literal-regexp)
const result = str.replace(re, "abc");
console.log(result);

Please help me to solved this.Thanking you in advance!

2 Answers
rules:
  - id: eslint.detect-non-literal-regexp
    patterns:
      - pattern: |
          new RegExp($ARG, ...)
      - pattern-not: |
          new RegExp("...", ...)

See how it's checking for the new RegExp($ARG, ...) pattern?

Here's how to circumvent the rule:

const pat = 'test';
const RE = RegExp;
const re = new RE(pat, 'i');
Related