我可以在JavaScript中编写正则表达式并使用.test()吗?

发布于 2025-02-02 21:29:45 字数 894 浏览 5 评论 0原文

好的,我对JavaScript非常新,并且有一个问题,请疑问使用中正则表达式。

我可以编写正则表达式并使用。 -test()这样的.test()作为基于消息bot的基于消息的响应以测试并根据匹配的方式给出这样的消息回复:

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");
    }

它似乎有效,但是在执行它时。但是由于某种原因,这感觉就像是在.test()之前每次指定myRegex的错误方式。


在此之前,我最初是在使用这样的语句,但是问题是在有更多单词时我必须写一条单独的消息,因为它只会检查确切的消息:

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

我注意到第二个代码仅适用于 “早上”或“早上”或“早上”之类的消息输入,但不会为“早上好”或“早上好”等。

诸如 想确保我做对了。

有人可以帮我吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岛歌少女 2025-02-09 21:29:45

您正在做的事情很好。唯一潜在的微小问题(仅是风格)是您不必要地重新分配变量。

如果您认为提前声明该模式并将其放入变量看起来很奇怪,那么您可以随意进行内联行动(如果,则可能需要使用else回复同一消息,以防多个测试匹配)

} 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");
}

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");
}
玩心态 2025-02-09 21:29:45

您可以检查字符串是否包含另一个字符串作为子字符串,其中 包括 方法:

if (msg.content.toLowerCase().includes('morning')) {
    // ...
}

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

if (msg.content.toLowerCase().includes('morning')) {
    // ...
}
苏佲洛 2025-02-09 21:29:45

要回答这个问题,您是否应该每次重新定义正则变量?简短的答案 - 不。

尽管这个特定示例没有任何明显的缺点,但是重新定义这样使用的变量可能会导致内容未知内容的错误。

我会嵌入正则表达式,尽管您还可以为每次支票创建一个新变量。这是一个夹住的正则示例:

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 ((/morning/i).test(msg.content)) { /* ... */ }

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)) { /* ... */ }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文