node.js - 发现 RegExp 构造函数的非文字参数 - Eslint 问题

标签 node.js regex visual-studio-code eslint

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

请帮我解决这个问题。在此先感谢您!

最佳答案

ESLint detect-non-literal-regexp rule explains其背后的逻辑:

Detects RegExp(variable), which might allow an attacker to DOS your server with a long-running regular expression.

More information: Regular Expression DoS and Node.js

如果您需要动态构建正则表达式,请禁用该规则并使用您的代码。也许实现 regex match cancel timeout feature .

如果您有一个像问题中那样的简单静态模式,请使用正则表达式文字表示法:

const re = /test/i;

关于node.js - 发现 RegExp 构造函数的非文字参数 - Eslint 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64480265/

相关文章:

javascript - apollo-client 可以在 node.js 上运行吗?

javascript - CSS3 文件 404 - - Node.JS 和 EJS

python - 您可以将列表成员的实例放入正则表达式中以在 python 中进行匹配吗?

Ruby 正则表达式 (regex) : character appear only once at most

node.js - Angular2嵌套路由不起作用,没有报告错误,但空白页

javascript - 范围错误: Invalid status code: 0 in Express App - Session issue?

java - 关于奇怪的 Java 正则表达式行为的问题

visual-studio-code - 打包和发布 VS Code 的步骤是什么?

visual-studio-code - VSCode 自动格式化为 2 个空格而不是 4 个

visual-studio-code - 如何修复 VS Code 中的错误文件描述符错误?