Skip to content

Latest commit

 

History

History
55 lines (35 loc) · 1.5 KB

prefer-regexp-test.md

File metadata and controls

55 lines (35 loc) · 1.5 KB
pageClass sidebarDepth title description since
rule-details
0
regexp/prefer-regexp-test
enforce that `RegExp#test` is used instead of `String#match` and `RegExp#exec`
v0.3.0

regexp/prefer-regexp-test

🔧 This rule is automatically fixable by the --fix CLI option.

enforce that RegExp#test is used instead of String#match and RegExp#exec

📖 Rule Details

This rule is aimed to use RegExp#test to check if a pattern matches a string.

This rule inspired by unicorn/prefer-regexp-test rule.

/* eslint regexp/prefer-regexp-test: "error" */

const text = 'something';
const pattern = /thing/;

/* ✓ GOOD */
if (pattern.test(text)) {}

/* ✗ BAD */
if (pattern.exec(text)) {}
if (text.match(pattern)) {}

🔧 Options

Nothing.

📚 Further reading

🚀 Version

This rule was introduced in eslint-plugin-regexp v0.3.0

🔍 Implementation