跳转到内容

useRegexpTest

此内容尚不支持你的语言。

biome.json
{
"linter": {
"rules": {
"nursery": {
"useRegexpTest": "error"
}
}
}
}

Enforce the use of RegExp.prototype.test() over String.prototype.match() and RegExp.prototype.exec() in boolean contexts.

When checking whether a string matches a regular expression, RegExp.prototype.test() is more appropriate than String.prototype.match() and RegExp.prototype.exec() because it returns a boolean directly. In contrast, match() and exec() return match objects or arrays, which involves unnecessary computation when only a true/false result is needed.

The fix is marked as unsafe because match() and exec() can have side effects when used with global or sticky regular expressions, since they advance the lastIndex property differently than test().

if ("hello world".match(/hello/)) {}
code-block.js:1:5 lint/nursery/useRegexpTest  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

.match() and .exec() are not designed for boolean checks.

> 1 │ if (“hello world”.match(/hello/)) {}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

These methods return match objects or arrays, which involves unnecessary computation when only checking for a match.

This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

Unsafe fix: Use .test() instead.

1 - if·(hello·world.match(/hello/))·{}
1+ if·(/hello/.test(hello·world))·{}
2 2

if (/hello/.exec("hello world")) {}
code-block.js:1:5 lint/nursery/useRegexpTest  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

.match() and .exec() are not designed for boolean checks.

> 1 │ if (/hello/.exec(“hello world”)) {}
^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

These methods return match objects or arrays, which involves unnecessary computation when only checking for a match.

This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

Unsafe fix: Use .test() instead.

1 - if·(/hello/.exec(hello·world))·{}
1+ if·(/hello/.test(hello·world))·{}
2 2

if (/hello/.test("hello world")) {}