-2

I was advised to use regex with this discord.js project. It saves two mentions from a message into two variables in the order the two mentions are typed. Discord.js reads mentions in the numeric order of the actual IDs, not the actual typed order, so we have to use regex instead. The command string is: f$command @user1 @user2 So, here's my code:

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

Is this correct, and how do I make it require 2 regex matches?

1 Answers1

0
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];
}
Scriptkiddy1337
  • 792
  • 4
  • 9