Skip to content

Prefer RegExp#test() over String#match() and RegExp#exec() ​

💼 This rule is enabled in the ✅ recommended config.

🔧💡 This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.

When you want to know whether a pattern is found in a string, use RegExp#test() instead of String#match() and RegExp#exec(), as it exclusively returns a boolean and therefore is more efficient.

Fail ​

js
if (string.match(/unicorn/)) {}
js
if (/unicorn/.exec(string)) {}
vue
<template>
	<div v-if="/unicorn/.exec(string)">Vue</div>
</template>

Pass ​

js
if (/unicorn/.test(string)) {}
vue
<template>
	<div v-if="/unicorn/.test(string)">Vue</div>
</template>

Released under the Apache License 2.0.