DougvF DougvF - 3 months ago 11
Node.js Question

Javascript nodejs & arrays

Basically I've been failing to create an array the way i want it, What do you think is the best method to create an array to output ALL regexp Matches that are found? Could you show me an example?

Here's an example of the output: which is passed from a variable called msg


id name ping snap updt posi dist

765611981281#### nameexampple1 600 0 0

765611981281#### nameexampple2 31 0 0

765611981281#### nameexampple3 41 0 0


Basically I want to match and output captures to array so only the ID/Name/Ping lines are indexed each on their own inside an array index that I can call later with the regex group like arraylist[2].highping[2] and that would output (nameexample2 because its the arraylist 2nd line) as highping regex group 2 is the username for that capture.

//RegExp
var pingchecker = new RegExp(/^([0-9]{0,17}).+?(\w+).+?([0-9].+).+?([0-9]).+?([0-9])/m); //set to multi line

//Finds RegExp of id name ping etc from msg which is the live feed to rcon
if (/^id.+?name.+?ping.+?snap.+?updt.+?posi.+?dist/.test(msg)) {
console.log('PING CHECKER - Passing to Conditional');

//Finds RegExp of pingchecker from msg which is the live feed to rcon
if((highping = pingchecker.exec(msg))) {
console.log('SteamID: ' + highping[1] + ' PlayerName: ' + highping[2] + ' Ping: ' + highping[3]) //outputs one player as normal.
}

}


Does this make sense to anyone? Am I going about this wrong? Basically i need pointers, above code is only output of the first found match for example.

Answer

I think you are looking for something like the following code. It will put each match array into an array of matches so you can "can call later with the regex group".

Changes:

  1. Change the pingchecker RegExp to match globally using /g. This allows it to match more than just once in the string.
  2. Change if to while so it loops through finding all matches instead of just the first one.
  3. Add variable resultArray to hold the results so, as requested in the question, "output captures to array" so you can "can call later with the regex group".
  4. Move console.log() into a separate resultsArray.forEach() loop to demonstrate that the resultsArray has been populated.

var msg= `id name ping snap updt posi dist

765611981281#### nameexampple1 600 0 0

765611981281#### nameexampple2 31 0 0

765611981281#### nameexampple3 41 0 0`;
    
//RegExp (multi line and global)
var pingchecker = new RegExp(/^([0-9]{0,17}).+?(\w+).+?([0-9].+).+?([0-9]).+?([0-9])/mg);
var resultArray =[];

//Finds RegExp of id name ping etc from msg which is the live feed to rcon
if (/^id.+?name.+?ping.+?snap.+?updt.+?posi.+?dist/.test(msg)) {
    console.log('PING CHECKER - Passing to Conditional');

    //Finds RegExp of pingchecker from msg which is the live feed to rcon
    //Loop through all matches, adding them to the OP requested resultsArray
    while(highping = pingchecker.exec(msg)) {
        resultArray.push(highping);
    }
}

//Loop through resultsArray and output the matches
resultArray.forEach(function(result){
     //output player
    console.log('SteamID: ' + result[1] + ' PlayerName: ' + result[2] 
                + ' Ping: ' + result[3]);
});

Comments