使用Regex定义与Discord.js变量?



我被建议在这个discord.js项目中使用正则表达式。它将消息中的两次提及按照两次提及的输入顺序保存到两个变量中。js以实际id的数字顺序读取提及,而不是实际的类型顺序,因此我们必须使用正则表达式代替。命令字符串:f$command @user1 @user2下面是我的代码:

else if (command === 'command'){
const regex = /<@!?(d+)>/;
let match = regex.exec(message);
while (match){
const User1 = match[1].id;
const User2 = match[2].id;
}

这是正确的,我如何使它需要2正则表达式匹配?

else if (command === 'command') {
const regex = /<@!?(d+)>/;
let match = regex.exec(message);
const result = [];
while (match){
result.push(match[1]);
match = regex.exec(message);
}
if (result.length !== 2) {
console.error('not 2 matches');
}
const User1 = result[0];
const User2 = result[1];
}

最新更新