繁体   English   中英

使用正则表达式定义带有 Discord.js 的变量?

[英]Using Regex to define variables with Discord.js?

有人建议我对这个 discord.js 项目使用正则表达式。 它将消息中的两个提及按两次提及的键入顺序保存到两个变量中。 Discord.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];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM