如果未提供全局标志(/g),推荐使用“RegExp#exec”,而不是“String#match”。
规则配置
// code-linter.json5
{
"rules": {
"@typescript-eslint/prefer-regexp-exec": "error"
}
} 正例
/thing/.exec('something');
'some things are just things'.match(/thing/g);
const text = 'something';
const search = /thing/;
search.exec(text); 反例
'something'.match(/thing/);
'some things are just things'.match(/thing/);
const text = 'something';
const search = /thing/;
text.match(search);