inux_pl
inux_pl

Reputation: 31

Match id from url using regex

I'm trying to select facebook fbid from url in Javascript but I have error while using "(?<=fbid=)" Links examples:

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

I would like to get: 2222222222

When I try:

var fbid1 = Link1.match(/fbid=[0-9]{10,16}/);
var fbid2 = Link1.match(/(?<=fbid=)[0-9]{10,16}/);

for fbid1 - I got: "fbid=2222222222"

for fbid2 - I got: error, button is not working, but on regex online generators I got what I want. Why ?

How to select only fbid from this urls? (without split in next step)

Upvotes: 0

Views: 745

Answers (1)

Jason S
Jason S

Reputation: 13779

Use a capture group for the part you want.

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

Or

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

Upvotes: 1

Related Questions