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

My string is like .html?request=516359075128086&to%5B0%5D=100004408050639&to%5B1%5D=1516147434&

My string is like .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.

My string is like https://www.facebook./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.

Share edited Jan 15, 2014 at 11:09 nhahtdh 56.8k15 gold badges129 silver badges164 bronze badges asked Jan 14, 2014 at 14:01 Delbin ThomasDelbin Thomas 3354 silver badges15 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5
var str = "https://www.facebook./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]);
}

Your regex specifies %5B0%5D which translate to [0]. The second user ID es with index 1 ([1]) which is encoded as %5B1%5D. If you want to accept any (numeric) user-Id-index, use '/to%5B\d+%5D=(.*?)&/g' as your regex which allows any number (\d+) as well as being global.

Regards.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745046345a4608120.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信