Home > Blockchain >  regex to match everything after second underscore
regex to match everything after second underscore

Time:05-25

i have this array

var collUnlocked = ['GRAVEL_1', 'GRAVEL_3', 'GRAVEL_2', 'GRAVEL_5', 'GRAVEL_4', 'GRAVEL_10', 'GRAVEL_-1', 'LOG:3_1', 'LOG:3_-1', 'LOG:3_3', 'LOG:3_4', 'LOG_2_6', 'LOG_2_2', 'LOG_2_3', 'LOG_2_-1']

i want to get the name (ex. LOG_2), and the last number, (ex. -1)

example: ['LOG:3_4', 'GRAVEL_-1', 'LOG_2_-1']

turns into -> [['LOG:3','4'], ['GRAVEL', '-1'], ['LOG_2', '-1']]

edit: im using this code to get the name, but i cant get the last number

collName = x.match('(.*\_)')[0];
collName = collName.slice(0, -1);

full code rn:

function testingAll() {
  var collUnlocked = ['GRAVEL_1', 'GRAVEL_3', 'GRAVEL_2', 'GRAVEL_5', 'GRAVEL_4', 'GRAVEL_10', 'GRAVEL_-1', 'LOG:3_1', 'LOG:3_-1', 'LOG:3_3', 'LOG:3_4', 'LOG_2_6', 'LOG_2_2', 'LOG_2_3', 'LOG_2_-1']

  getCollLevelssss(collUnlocked);
}

function getCollLevelssss(collUnlocked) {
  var collName;
  var collLevel;

  collUnlocked.forEach(x => {
    collName = x.match('(.*\_)')[0];
    collName = collName.slice(0, -1);

    collLevel = x.match(''); //need regex right here

    console.log(x   ' | '   collName   ' = '   collLevel);
  });
}

CodePudding user response:

In your goal, how about the following approach? This sample script uses split().

Sample script:

var collUnlocked = ['GRAVEL_1', 'GRAVEL_3', 'GRAVEL_2', 'GRAVEL_5', 'GRAVEL_4', 'GRAVEL_10', 'GRAVEL_-1', 'LOG:3_1', 'LOG:3_-1', 'LOG:3_3', 'LOG:3_4', 'LOG_2_6', 'LOG_2_2', 'LOG_2_3', 'LOG_2_-1']
var res = collUnlocked.map(e => {
  var temp = e.split("_");
  var last = temp.pop();
  return [temp.join("_"), last];
});
console.log(res)

  • When this script is run, the following result is obtained.

      [ 
        [ 'GRAVEL', '1' ],
        [ 'GRAVEL', '3' ],
        [ 'GRAVEL', '2' ],
        [ 'GRAVEL', '5' ],
        [ 'GRAVEL', '4' ],
        [ 'GRAVEL', '10' ],
        [ 'GRAVEL', '-1' ],
        [ 'LOG:3', '1' ],
        [ 'LOG:3', '-1' ],
        [ 'LOG:3', '3' ],
        [ 'LOG:3', '4' ],
        [ 'LOG_2', '6' ],
        [ 'LOG_2', '2' ],
        [ 'LOG_2', '3' ],
        [ 'LOG_2', '-1' ]
      ]
    

Reference:

CodePudding user response:

Split and reassemble

function myfunk() {
  let a = ['GRAVEL_1', 'GRAVEL_3', 'GRAVEL_2', 'GRAVEL_5', 'GRAVEL_4', 'GRAVEL_10', 'GRAVEL_-1', 'LOG:3_1', 'LOG:3_-1', 'LOG:3_3', 'LOG:3_4', 'LOG_2_6', 'LOG_2_2', 'LOG_2_3', 'LOG_2_-1'];
  let b = a.reduce((a,c) => {
    let t = c.split('_');
    if(t.length == 2) {
      a.arr.push(`${t[0]},${t[1]}`);
    } else if(t.length == 3) {
      a.arr.push(`${t[0]}_${t[1]},${t[2]}`);
    }
    return a;
  },{arr:[]}).arr;
  Logger.log(JSON.stringify(b));
}

Execution log
7:10:50 PM  Notice  Execution started
7:10:50 PM  Info    ["GRAVEL,1","GRAVEL,3","GRAVEL,2","GRAVEL,5","GRAVEL,4","GRAVEL,10","GRAVEL,-1","LOG:3,1","LOG:3,-1","LOG:3,3","LOG:3,4","LOG_2,6","LOG_2,2","LOG_2,3","LOG_2,-1"]
7:10:51 PM  Notice  Execution completed
  • Related