0

Okay, I am very new to javascript and have a question regarding using regular expressions in it.

Can I write regular expressions and use .test() like this for a message based response of a discord bot to test and give a message reply like this based on if it matches:

let myRegex = /morning/i;
    if (myRegex.test(msg.content)) {
       msg.reply("Good Morning!");
    }

    myRegex = /some text/i;
    if (myRegex.test(msg.content)) { 
       msg.reply("Bot replies to some text");
    }

    myRegex = /some other text/i;
    if (myRegex.test(msg.content)) { 
       msg.reply("bot replies to some other text");
    }

It seems to work but while executing it. But for some reason, it feels like this is the wrong way of doing things as I am specifying myRegex each time before the .test().


Before this, I was initially using if statements like this but the issue there was that I had to write a separate message when there were more words as it would only check for that exact message:

if (msg.content.toLowerCase() === 'morning') {
          msg.reply('https://media.giphy.com/media/34dapC0zP8iSQ0wjHX/giphy.gif ');
    }

I have noticed that the second code would only work for message inputs such as 'morning' or 'MoRnING' or 'MORNING', but wouldn't work for 'good morning' or 'GooD MoRnIng' 'GOOD MORNING' etc.

So, I am thinking of moving to regular expressions, but I want to make sure that I am doing it right.

Can someone please help me out with this?

tribelord
  • 3
  • 3

3 Answers3

1

What you're doing is perfectly fine. The only potential tiny issue (which is only stylistic) is that you're unnecessarily reassigning a variable.

If you think declaring the pattern ahead of time and putting it into a variable looks weird, you're free to do it inline instead (and you might want to use else if so that there aren't multiple replies to the same message in case it matches multiple tests)

} else if (/some text/i.test(msg.content)) { 
   msg.reply("Bot replies to some text");
} else if (/some other text/i.test(msg.content)) { 
   msg.reply("bot replies to some other text");
}
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • Yes perfect, thank you for the comment. This definitely looks a lot better than reassigning new values to the variable. I am going to give this a try. About the multiple replies thing, it's funny because I just faced something like that while trying out one of the other comment's suggestion with the .includes() . – tribelord May 30 '22 at 01:10
0

You can check if a string includes another string as a substring with the includes method:

if (msg.content.toLowerCase().includes('morning')) {
    // ...
}
kelsny
  • 23,009
  • 3
  • 19
  • 48
  • I tried this. Thats cool suggestion but it seems like the bot started replying to itselfin an endless chain after that since the reply 'Good morning' had the 'morning' in it! xD Also what are your thoughts on using regular expression instead? – tribelord May 30 '22 at 00:57
0

To answer the question, should you be redefining the RegEx variable each time? Short answer - No.

Though there isn't any obvious downside to this specific example, redefining variables that are used like this can lead to bugs with unknown content.

I would inline the regular expressions, though you could also create a new variable for each check. Here's an example with the inlined regexes:

if (/morning/i.test(msg.content)) {
  msg.reply("Good Morning!");
}
if (/some text/i.test(msg.content)) {
  msg.reply("Bot replies to some text");
}
if (/some other text/i.test(msg.content)) {
  msg.reply("Bot replies to some other text");
}

If you prefer, you could wrap the regex within parenthesis.

if ((/morning/i).test(msg.content)) { /* ... */ }
Jacob
  • 1,577
  • 8
  • 24
  • 1
    >Though there isn't any obvious downside to this specific example, redefining variables that are used like this can lead to bugs with unknown content. Thank you for your comment. This part is exactly what my concern was. You've put it in the right words. Redefining seemed a bit wrong to me as it could potentially create issues. I think the way you've mentioned here without the use of a variable seems elegant and I will give it a try right now and come back, but it does look better and not odd. – tribelord May 30 '22 at 01:06