diff --git a/packages/eslint-plugin/src/rules/prefer-regexp-exec.ts b/packages/eslint-plugin/src/rules/prefer-regexp-exec.ts index ebb92364e464..bbb7d9aac746 100644 --- a/packages/eslint-plugin/src/rules/prefer-regexp-exec.ts +++ b/packages/eslint-plugin/src/rules/prefer-regexp-exec.ts @@ -84,6 +84,7 @@ export default createRule({ ) { const [, flags] = node.arguments; return ( + flags && flags.type === AST_NODE_TYPES.Literal && typeof flags.value === 'string' && flags.value.includes('g') diff --git a/packages/eslint-plugin/tests/rules/prefer-regexp-exec.test.ts b/packages/eslint-plugin/tests/rules/prefer-regexp-exec.test.ts index 8a5bdfd099db..2ac3c7a07ba7 100644 --- a/packages/eslint-plugin/tests/rules/prefer-regexp-exec.test.ts +++ b/packages/eslint-plugin/tests/rules/prefer-regexp-exec.test.ts @@ -230,6 +230,33 @@ function test(pattern: string) { output: ` function test(pattern: string) { new RegExp(pattern, undefined).exec('check'); +} + `, + }, + { + // https://github.com/typescript-eslint/typescript-eslint/issues/3941 + code: ` +function temp(text: string): void { + text.match(new RegExp(\`\${'hello'}\`)); + text.match(new RegExp(\`\${'hello'.toString()}\`)); +} + `, + errors: [ + { + messageId: 'regExpExecOverStringMatch', + line: 3, + column: 8, + }, + { + messageId: 'regExpExecOverStringMatch', + line: 4, + column: 8, + }, + ], + output: ` +function temp(text: string): void { + new RegExp(\`\${'hello'}\`).exec(text); + new RegExp(\`\${'hello'.toString()}\`).exec(text); } `, },