Skip to content
← Back to rules

unicorn/prefer-regexp-test 厳格な

🛠️ An auto-fix is available for this rule.

何をするか

String#match()String#exec() の代わりに、RegExp#test() を推奨します。

なぜ問題なのか

文字列内にパターンが見つかったかどうかを確認したい場合、 String#match()RegExp#exec() の代わりに RegExp#test() を使用すべきです。これは真偽値のみを返すため、より効率的です。

このルールに違反する誤ったコードの例:

javascript
if (string.match(/unicorn/)) {
}
if (/unicorn/.exec(string)) {
}

このルールに準拠する正しいコードの例:

javascript
if (/unicorn/.test(string)) {
}
Boolean(string.match(/unicorn/));

使用方法

設定ファイルまたは CLI でこのルールを有効化するには、以下のように使用できます:

json
{
  "rules": {
    "unicorn/prefer-regexp-test": "error"
  }
}
bash
oxlint --deny unicorn/prefer-regexp-test

参照