我正在使用不和谐my在NodeJS中构建自己的不和谐机器人。使用下面的代码,您可以识别消息是否只包含一个表情符号。
client.on("message", function(message){
var bool = message.content.match(/^(:[^:\s]+:|<:[^:\s]+:[0-9]+>|<a:[^:\s]+:[0-9]+>)$/);当您执行一个console.log时,一个表情显示如下(例如Kappa的表情):
<:Kappa:731053321502326797>
现在,它只匹配当一个表情在消息中,但它不匹配,如果一个消息包含两个表情之间的空格。
我怎么才能让这一切成为可能?
发布于 2020-07-17 18:47:02
使用
^(:[^:\s]+:|<:[^:\s]+:[0-9]+>|<a:[^:\s]+:[0-9]+>)+$见证明。
解释:
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发布于 2020-07-17 21:49:34
您需要正则表达式的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有表情符号和其他文字的输入是否应该失败还不清楚。此版本允许组合表情符号和其他文本。
https://stackoverflow.com/questions/62955907
复制相似问题