unicorn/prefer-string-starts-ends-with 正确性
它做了什么
更倾向于使用 String#startsWith() 和 String#endsWith() 来替代带有 /^foo/ 或 /foo$/ 的正则表达式。
为什么这么糟糕?
使用 String#startsWith() 和 String#endsWith() 更具可读性和性能,因为它不需要解析正则表达式。
示例
以下是针对此规则的错误代码示例
javascript
const foo = "hello";
/^abc/.test(foo);以下是针对此规则的正确代码示例
javascript
const foo = "hello";
foo.startsWith("abc");