Skip to content

Latest commit

Β 

History

History
35 lines (24 loc) Β· 1.39 KB

File metadata and controls

35 lines (24 loc) Β· 1.39 KB

prefer-regexp-test

πŸ“ Prefer RegExp#test() over String#match() and RegExp#exec().

πŸ’Ό This rule is enabled in the following configs: βœ… recommended, β˜‘οΈ unopinionated.

πŸ”§πŸ’‘ 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.

Examples

// ❌
if (string.match(/unicorn/)) {}

// ❌
if (/unicorn/.exec(string)) {}

// βœ…
if (/unicorn/.test(string)) {}
<template>
	<!-- ❌ -->
	<div v-if="/unicorn/.exec(string)">Vue</div>

	<!-- βœ… -->
	<div v-if="/unicorn/.test(string)">Vue</div>
</template>