我的字符串就像https://www.facebook.com/connect/login_success.html?request=516359075128086&to%5B0%5D=100004408050639&to%5B1%5D=1516147434&_=_
此字符串是发送到 fb 的应用邀请 url 重定向请求的结果。
我想做的是,将用户 ID 100004408050639 和 1516147434 提取到数组中。
我试过 var fbCode = testString.match(/to%5B0%5D=(.*)&/);
但这只返回这些数字中的一个,即第一次出现。
最佳答案
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");
当对 global search 使用 g
修饰符时并希望获得 (
组 )
的匹配项,可以遍历匹配项。第一个括号内的组将在 [1]
var matches = [];
while(matches = re.exec(str)) {
console.log(matches[1]);
}
关于Javascript:如何使用正则表达式从字符串中提取多个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21115455/