discordjs / nodejs,如何检查消息是否仅包含自定义表情?

时间:2020-07-17 14:17:46

标签: node.js regex discord.js

我正在使用discordjs在NodeJS中构建自己的discordbot。 通过下面的代码,您可以识别邮件中是否仅包含表情符号

client.on("message", function(message){
var bool = message.content.match(/^(:[^:\s]+:|<:[^:\s]+:[0-9]+>|<a:[^:\s]+:[0-9]+>)$/);

执行console.log时,表情会显示如下(例如,Kappa表情):

<:kappa:731053321502326797>

现在,它仅在消息中包含1个表情符时才匹配 但是,如果一条消息包含2个表情并在其之间留有空格,则表示不匹配。

我如何做到这一点?

2 个答案:

答案 0 :(得分:2)

使用

^(:[^:\s]+:|<:[^:\s]+:[0-9]+>|<a:[^:\s]+:[0-9]+>)+$

请参见proof

说明:

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (                        group and capture to \1 (1 or more times
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    :                        ':'
--------------------------------------------------------------------------------
    [^:\s]+                  any character except: ':', whitespace
                             (\n, \r, \t, \f, and " ") (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    :                        ':'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    <:                       '<:'
--------------------------------------------------------------------------------
    [^:\s]+                  any character except: ':', whitespace
                             (\n, \r, \t, \f, and " ") (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    :                        ':'
--------------------------------------------------------------------------------
    [0-9]+                   any character of: '0' to '9' (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    >                        '>'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    <a:                      '<a:'
--------------------------------------------------------------------------------
    [^:\s]+                  any character except: ':', whitespace
                             (\n, \r, \t, \f, and " ") (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    :                        ':'
--------------------------------------------------------------------------------
    [0-9]+                   any character of: '0' to '9' (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    >                        '>'
--------------------------------------------------------------------------------
  )+                       end of \1 (NOTE: because you are using a
                           quantifier on this capture, only the LAST
                           repetition of the captured pattern will be
                           stored in \1)
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

答案 1 :(得分:0)

您需要用于正则表达式的g(全局)修饰符。以下是一些要测试的输入字符串:

var strings = [
    '<:Kappa:731053321502326797>',
    '<:Kappa:731053321502326797> :smile:',
    'text with :smile:',
    'something else'
];
var re = /(:[^:\s]+:|<:[^:\s]+:[0-9]+>|<a:[^:\s]+:[0-9]+>)/g;
strings.forEach((str) => {
    var matches = str.match(re);
    console.log(str + '\n==> matches: ' + JSON.stringify(matches));
});

输出:

<:Kappa:731053321502326797>
==> matches: ["<:Kappa:731053321502326797>"]
<:Kappa:731053321502326797> :smile:
==> matches: ["<:Kappa:731053321502326797>",":smile:"]
text with :smile:
==> matches: [":smile:"]
something else
==> matches: null

不清楚带有表情符号和其他文本的输入是否应该失败。此版本允许组合表情符号和其他文本。