Javascript: How to extract multiple values from a string using regex?

facebook, facebook-javascript-sdk, javascript, regex

Solution

var str = "https://www.facebook.com/connect/login_success.html?request=516359075128086&to%5B0%5D=100004408050639&to%5B1%5D=1516147434&_=_";

var re = new RegExp("to%5B[01]%5D=(\\d+)", "g");

When using the `g` modifier for global search and want to get matches for a `(` group `)`, could loop through the matches. Those for the first parenthesized group will be in `[1]`

var matches = [];

while(matches = re.exec(str)) {
  console.log(matches[1]);
}

Problem

My string is like `https://www.facebook.com/connect/login_success.html?request=516359075128086&to%5B0%5D=100004408050639&to%5B1%5D=1516147434&_=_` This string is result of a app invitation url redirect request sent to fb. And what i want to do is, to extract user ids 100004408050639 and 1516147434 to an array. I tried `var fbCode = testString.match(/to%5B0%5D=(.*)&/);` but this returns only one of these numbers, ie first occurrence.

Original source