Вы можете создать массив ключевых слов и использовать some
, чтобы проверить, существует ли хотя бы одно из них в строке, например:
const caption = "I love dogs";
const animals = ["dog", "fish", "bird", "cat"];
const exists = animals.some(animal => caption.includes(animal))
if (exists) {
console.log("Yes");
// notify
}
Или вы можете использовать регулярное выражение, например:
const animals = ["dog", "fish", "bird", "cat"];
const regex = new RegExp(animals.join("|")) // animals seperated by a pipe "|"
if (regex.test("I love cats")) {
console.log("Yes");
}
// if you don't want to create an array then:
if (/dog|fish|bird|cat/.test("I love elephants")) {
console.log("Yes");
} else {
console.log("No")
}