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

Javascript: error Unnecessary use of boolean literals in conditional expression no-unneeded-ternary

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

error  Unnecessary use of boolean literals in conditional expression  no-unneeded-ternary

这是我的代码:

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;
};

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

你可以取 RegExp#test 其中 returns 一个布尔值。

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

condition ? true : false;真奇怪

使用Boolean(condition)!!condition

转换布尔类型

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})$/);
    return Boolean(match);
};