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?