PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 9, 2022

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

 July 09, 2022     java, javascript, keyword, node.js     No comments   

Issue

I am writing a program that looks for specific keywords and then only performs an action when a keyword is present in a message. I am having difficulty trying to make it so that it will pickup on keywords regardless of their capitalization. Below is an example of what I currently have.

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

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

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

The only way I've figured out how to do this is to add each variation to the keyword list. How would I make it so that it could detect for example "apple" or "BaNaNa" without adding those variations to the keyword list?


Solution

If your message is a string, just make the whole thing lower case and match it to lowercase keywords.

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


Answered By - Ed Lucas
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing