javascript – How to use match() in if else condition correctly. (node.js)

I have dynamic response data that either has a completely random string, or a string like Johnny likes to eat {food} for breakfast data as I’m trying to match.

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
}

If the statement is false, it would just log null and not to the else. How can I make it that it would?

Read more here: Source link