Javascript:错误在条件表达式中不必要地使用布尔文字 no-unneeded-ternary

Oli*_*dot 1 javascript regex email if-statement conditional-operator

我是 javascript 新手,我似乎无法解决我遇到的一个小问题。我四处寻找,尝试了许多其他选择,但似乎没有任何效果。此功能工作正常,但我收到此错误消息:

error  Unnecessary use of boolean literals in conditional expression  no-unneeded-ternary
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

const valid = (email) => {
  // TODO: return true if the `email` string has the right pattern!
  const match = (email.match(/^([a-zA-Z0-9_\-.]+)@([a-zA-Z0-9_\-.]+)\.([a-zA-Z]{2,5})$/) ? true : false);
  return match;
};
Run Code Online (Sandbox Code Playgroud)

有谁知道我怎么能用不同的方式写这个?预先感谢您的帮助!奥利维尔

Nin*_*olz 5

您可以使用RegExp#testwhich 返回一个布尔值。

const valid = email => /^([a-zA-Z0-9_\-.]+)@([a-zA-Z0-9_\-.]+)\.([a-zA-Z]{2,5})$/.test(email);
Run Code Online (Sandbox Code Playgroud)