Zulkifli Said
Zulkifli Said

Reputation: 631

Regex Check Facebook Video URL

I try to check facebook video url using regex.

this is example Valid fb video URL :
https://www.facebook.com/video.php?v=100000000000000 (VALID)

this is example Valid fb video URL with username : https://www.facebook.com/{username}/videos/100000000000000

note : {username} can contain any string. example : https://www.facebook.com/username1/videos/100000000000000 (VALID) https://www.facebook.com/username2/videos/100000000000000 (VALID)

But my reqex still wrong if i check fb video url with username.
This is my regex :

^http(s)?://(www\.)?facebook.([a-z]+)/(?!(?:video\.php\?v=\d+|usernameFB/videos/\d+)).*$

You can run it : https://regex101.com/r/dF5iP1/6

Upvotes: 3

Views: 8059

Answers (6)

AMMAR YASIR
AMMAR YASIR

Reputation: 169

That will help you regexr.com/4tdur

you can use like this

const myURL =  "https://www.facebook.com/video.php?v=100000000000000";
const res = /^https?:\/\/www\.facebook\.com.*\/(video(s)?|watch|story)(\.php?|\/).+$/gm.test(myURL);
console.log(res);

Upvotes: 1

Leo
Leo

Reputation: 425

Latest:

/(?:https?:\/\/)?(?:www.|web.|m.)?(facebook|fb).(com|watch)\/(?:video.php\?v=\d+|(\S+)|photo.php\?v=\d+|\?v=\d+)|\S+\/videos\/((\S+)\/(\d+)|(\d+))\/?/

Upvotes: 1

Himanshu Aggarwal
Himanshu Aggarwal

Reputation: 1809

The Facebook Video URLs nowadays are of the formats as following:-

https://www.facebook.com/NowThisPolitics/videos/968643940204333/ https://www.facebook.com/chandni.nathani2/videos/10158204539960536/UzpfSTEwMDAwMTc3MzU1MjI2NzoyNzMxNDUyMTYzNTkwNTQy/

Also, since the facebook could be replaced by fb, I created this regex:

/(?:https?:\/{2})?(?:w{3}\.)?(facebook|fb).com\/.*\/videos\/.*/

Upvotes: -1

zurfyx
zurfyx

Reputation: 32807

UPDATED October 2018

Neither of the two existing REGEX proposals worked for me, and there are more visible cases than the ones considered.

Here's my REGEX Proposal:

^(?:(?:https?:)?\/\/)?(?:www\.)?facebook\.com\/[a-z\.]+\/videos\/(?:[a-z0-9\.]+\/)?([0-9]+)\/?(?:\?.*)?$

^(?:(?:https?:)?\/\/)?(?:www\.)?facebook\.com\/[a-zA-Z0-9\.]+\/videos\/(?:[a-zA-Z0-9\.]+\/)?([0-9]+)

I ignored video.php, I think it's old enough to safely ignore it.

Matches:

https://www.facebook.com/aguardos.nocturnos/videos/vb.1614866072064590/1828228624061666/?type=2&theater https://www.facebook.com/aguardos.nocturnos/videos/vb.1614866072064590/1828228624061666?type=2&theater https://www.facebook.com/aguardos.nocturnos/videos/1828228624061666/ https://www.facebook.com/latavernadelssomnis/videos/1609038972452561/?hc_ref=NEWSFEED //www.facebook.com/aguardos.nocturnos/videos/1828228624061666/ https://facebook.com/aguardos.nocturnos/videos/1828228624061666/ http://www.facebook.com/aguardos.nocturnos/videos/1828228624061666/ www.facebook.com/aguardos.nocturnos/videos/18282286240612666/ facebook.com/aguardos.nocturnos/videos/18282286240612666/ https://www.facebook.com/aguardos.nocturnos/videos/1828228624061666 https://www.facebook.com/WEAU13News/videos/588612391555522/UzpfSTEzMzAzMDk4NjM6MTAyMTMxMjMzNDE3ODE0MTI/

I do not own nor I have watched any of the videos. I just picked random ones that were on my facebook feed.

Groups

  1. Video ID.

Gotchas

One of the most common Facebook video formats is more complex than I'd like it to be and matching every case perfectly with REGEX would probably lead to a very messy query.

https://www.facebook.com/RolandGarros/videos/10155404760334920/FOO (valid)

https://www.facebook.com/RolandGarros/videos/FOO/10155404760334920 (valid)

https://www.facebook.com/RolandGarros/videos/10155404760334920/FOO/FOO (invalid)

The way this one seems to work is by retrieving the numeric value in the first or second part after videos/.

https://www.facebook.com/RolandGarros/videos/10155361533554920/1015536153355492134

What about this one where two valid numeric values are involved? It seems like the second one is the one that will prevail.

For this reason the REGEX solution above was softened1 to match only the beginning of the Facebook URL, up to the video group that we're looking for. Considering that your goal's probably to extract the video ID, rather than verify the URL, I think that's a valid trade-off. At the end of the day, you'll be checking the video either way (either through API or scrapping) to extract the video information since an ID doesn't mean that the video exists or it's public.

1 Not just softened, but also improved to match the test case format.

Test

You can easily test it yourself @ Regex101

Upvotes: 3

Phil Walton
Phil Walton

Reputation: 963

This is a little different than Pedro's, but it works well.

^http(?:s)?://(?:www\.)?facebook.(?:[a-z]+)/((?:video\.php\?v=\d+|username\d/videos/\d+)).*$

https://regex101.com/r/nV4rI3/1

Upvotes: 1

Pedro Lobito
Pedro Lobito

Reputation: 99011

This will work for you:

^(https?://www\.facebook\.com/(?:video\.php\?v=\d+|.*?/videos/\d+))$

Demo

https://regex101.com/r/sC6oR2/3

Upvotes: 4

Related Questions