简体   繁体   中英

How can I capture multiple groups, multiple times with javascript regex

I have this code snippet aiming to extract next and last link values from github api...

var types = {},
    str = '<https://api.github.com/repositories/1424470/issues?access_token=f554f90b4c95c78091a4202150d4583082dab1ce&page=2>; rel="next", <https://api.github.com/repositories/1424470/issues?access_token=f554f90b4c95c78091a4202150d4583082dab1ce&page=7>; rel="last"',
    rex = /\s*<https?:\/\/api.github.com\/.+?&page=(\d+)>;\s*rel="(\w+?)"(?:,|$)/g;

// use regex replace method to capture multiple groups multiple times
str.replace(rex, function(_, page, type){
    types[type] = +page;
});

console.log(types);
// {next: 2, last: 7}

It is functioning correctly, but feels like a mis-use of regex replace method, where I am not returning anything, but using it only for the sake of having a callback for each match, which I use to build up an output object.

I would prefer some kind of matchAll, returning multi-dimensional array of matches, and parts.

Is there a better way to handle this case in javascript?

You can use the exec() method in a loop, pushing the match results to a multi-dimensional array.

function find_all(re, s) {
   var types = [];
   while (m = re.exec(s)) {
        types.push([m[2], m[1]])
   }
   return types;
}

var regex = new RegExp('<https?://[^>]+page=(\\d+)>;\\s*rel="([^"]+)"', 'gi');

find_all(regex, str) //=> [ [ 'next', '2' ], [ 'last', '7' ] ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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