无论大小写如何检查关键字是否存在?

How to check if a keyword is present regardless of capitalization?

我正在编写一个程序来查找特定关键字,然后仅在消息中出现关键字时才执行操作。我很难尝试使它无论大小写如何都能提取关键字。下面是我目前拥有的示例。

for (var i = 0; i < keyword.length; i++) {

      if (msg.content.includes (keyword[i])) {
      msg.channel.send("Orange")
      }

var keyword = ["Apple","Banana"] 

我想出的唯一方法是将每个变体添加到关键字列表中。我将如何做到它可以检测例如 "apple" 或 "BaNaNa" 而无需将这些变体添加到关键字列表?

将数据库和搜索词都转换为小写。

您可以将消息和关键字都转换为小写并检查消息中的现有关键字。

if (msg.content.toLowerCase().includes (keyword[i]).toLowerCase()) {
  msg.channel.send("Orange")
}

如果您的消息是一个字符串,只需将整个内容设为小写并将其与小写关键字匹配。

let message = msg.content.toLowerCase();
if (message.includes(keyword[i].toLowerCase()) {
    ....
}

如果我有一个大字符串,'Hello Marco, how are you doing?',还有另一个字符串,'MarCo',我想检查第二个字符串是否在第一个字符串中,我会做

const needle = 'MarCo';
const haystack = 'Hey Marco, how are you doing?'

const inc = haystack.toLowerCase().includes(needle.toLowerCase());
if (inc) {
  console.log('its in there')
} else {
  console.log('not in there');
}