useRegexpTest
このコンテンツはまだ日本語訳がありません。
Summary
Section titled “Summary”- Diagnostic Category:
lint/nursery/useRegexpTest - This rule has an unsafe fix.
- The default severity of this rule is information.
- Sources:
- Same as
unicorn/prefer-regexp-test
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useRegexpTest": "error" } } }}Description
Section titled “Description”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().
Examples
Section titled “Examples”Invalid
Section titled “Invalid”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")) {}Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.