You need to loop because match won't grab capture groups in a global regex, so you'll end up having some extra character you don't need:
var url = '/login/:id/:name';
var res = [];
url.replace(/:(\w+)/g, function(_, match) {
res.push(match);
});
console.log(res); //=> ["id", "name"]
You can also use this helper:
String.prototype.gmatch = function(regex) {
var result = [];
this.replace(regex, function() {
var matches = [].slice.call(arguments, 1, -2);
result.push.apply(result, matches);
});
return result;
};
var res = url.gmatch(/:(\w+)/g); //=> ["id", "name"]
substrto check the patterns, but I'm hoping for something cleaner.