3 回答

TA貢獻(xiàn)1802條經(jīng)驗 獲得超5個贊
你可以用普通的 Javascript 來完成。嘗試
const regex = /foo/;
const testString = "bar";
if(regex.test(testString)) {
// code..
}

TA貢獻(xiàn)1860條經(jīng)驗 獲得超9個贊
_.invoke是要使用的正確函數(shù),但您的語法稍有錯誤??匆幌挛臋n:使用(的第三個參數(shù)invoke)調(diào)用方法的參數(shù)不在數(shù)組中,在您的情況下,path參數(shù)(的第二個參數(shù)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貢獻(xiàn)1805條經(jīng)驗 獲得超9個贊
你為什么不為此使用 vanilla JS?
var regex = /foo/;
var string = "foo";
console.log(regex.test(string))
添加回答
舉報