首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不和谐的if /nodejs,如何检查消息是否只包含自定义的表情?

不和谐的if /nodejs,如何检查消息是否只包含自定义的表情?
EN

Stack Overflow用户
提问于 2020-07-17 14:17:46
回答 2查看 1.3K关注 0票数 1

我正在使用不和谐my在NodeJS中构建自己的不和谐机器人。使用下面的代码,您可以识别消息是否只包含一个表情符号。

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

当您执行一个console.log时,一个表情显示如下(例如Kappa的表情):

<:Kappa:731053321502326797>

现在,它只匹配当一个表情在消息中,但它不匹配,如果一个消息包含两个表情之间的空格。

我怎么才能让这一切成为可能?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-07-17 18:47:02

使用

代码语言:javascript
复制
^(:[^:\s]+:|<:[^:\s]+:[0-9]+>|<a:[^:\s]+:[0-9]+>)+$

证明

解释:

代码语言:javascript
复制
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
票数 2
EN

Stack Overflow用户

发布于 2020-07-17 21:49:34

您需要正则表达式的g (全局)修饰符。下面是几个要测试的输入字符串:

代码语言:javascript
复制
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));
});

输出:

代码语言:javascript
复制
<:Kappa:731053321502326797>
==> matches: ["<:Kappa:731053321502326797>"]
<:Kappa:731053321502326797> :smile:
==> matches: ["<:Kappa:731053321502326797>",":smile:"]
text with :smile:
==> matches: [":smile:"]
something else
==> matches: null

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62955907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档