unicorn/prefer-regexp-test 迂腐的
功能
将 RegExp#test() 优先于 String#match() 和 String#exec()。
为什么不好?
在你想知道一个模式是否在字符串中时,使用 RegExp#test() 而不是 String#match() 和 RegExp#exec(),因为它只返回一个布尔值,因此更有效。
示例
此规则的不正确代码示例
javascript
if (string.match(/unicorn/)) {
}
if (/unicorn/.exec(string)) {
}此规则的正确代码示例
javascript
if (/unicorn/.test(string)) {
}
Boolean(string.match(/unicorn/));