使用正则表达式匹配来自网址的ID

时间:2016-02-22 02:03:53

标签: javascript regex

我正在尝试从Javascript中的url中选择facebook fbid但是在使用“(?< = fbid =)”时出错 链接示例:

ItemStream

我想得到:2222222222

当我尝试:

var Link1 = www.facebook.com/111111111111/fbid=2222222222?1111111111
var Link2 = www.facebook.com/posts/fbid=2222222222

对于fbid1 - 我得到了:“fbid = 2222222222”

对于fbid2 - 我得到:错误,按钮不起作用,但在正则表达式在线生成器我得到了我想要的。为什么?

如何从此网址中仅选择fbid? (下一步没有拆分)

1 个答案:

答案 0 :(得分:1)

为所需部件使用捕获组。

var match = Link1.match(/fbid=([0-9]{10,16})/),
    fbid = null;
if(match !== null) {
    fbid = match[1];
}

var match = Link1.match(/fbid=([0-9]{10,16})/),
    fbid = match !== null ? match[1] : null;
相关问题