我是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;
};有人知道我怎么能写出不同的东西吗?提前感谢您的帮助!奥利维尔
发布于 2020-05-05 15:54:44
您可以使用返回布尔值的RegExp#test。
const valid = email => /^([a-zA-Z0-9_\-.]+)@([a-zA-Z0-9_\-.]+)\.([a-zA-Z]{2,5})$/.test(email);发布于 2020-05-05 15:55:05
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);
};https://stackoverflow.com/questions/61608232
复制相似问题