【发布时间】:2021-09-30 08:30:27
【问题描述】:
我有动态响应数据,要么是完全随机的字符串,要么是像 Johnny likes to eat {food} for breakfast 这样的字符串,因为我正在尝试匹配。
var data = response.body
if (data.match(/Johnny likes to eat(.*)for breakfast/)[0]) {
console.log("Johnny in fact likes to eat" + data.match(/Johnny likes to eat(.*)for breakfast/)[1])
} else {
console.log("Johnny eats nothing") //this doesnt get executed, just logs null
}
如果语句为假,它只会记录null,而不是else。我怎样才能做到这一点?
【问题讨论】:
-
“约翰尼喜欢在早餐时吃 {food}”总是这个结构吗?
标签: javascript node.js if-statement dynamic match