3 回答

TA贡献1802条经验 获得超5个赞
你可以用普通的 Javascript 来完成。尝试
const regex = /foo/;
const testString = "bar";
if(regex.test(testString)) {
// code..
}

TA贡献1860条经验 获得超9个赞
_.invoke是要使用的正确函数,但您的语法稍有错误。看一下文档:使用(的第三个参数invoke)调用方法的参数不在数组中,在您的情况下,path参数(的第二个参数invoke)也是一个简单的字符串。这是如何做到的:
// A string
let string = "foo";
// A matching regex (not the case in the original post)
let regex = /foo/;
// Does it match? Note there's two pairs of []
// that shouldn't be there in the original post.
let result = _.invoke(string, "match", regex);
// ["foo"]
console.log(result);

TA贡献1805条经验 获得超9个赞
你为什么不为此使用 vanilla JS?
var regex = /foo/;
var string = "foo";
console.log(regex.test(string))
添加回答
举报