Code King
Code King

Reputation: 45

Facebook regex allow . and ?fref=ufi

I have been using the following regex, to check if it is a valid facebook url: /^(?:(?:http|https):\/\/)?(?:www.)?facebook.com\/(?:(?:\w)*#!\/)?(?:pages\/)?(?:[?\w\-]*\/)?(?:profile.php\?id=(?=\d.*))?([\w\-]*)?$/

The problem is that, this regex is old, and now some facebook url's has a . and ?fref=ufi in there url example: https://www.facebook.com/user.moreusername?fref=ufiHow can i fix so that this regex accept that kind of url?

I have been trying to add \/?(?:?fref=ufi.)?\/ Without any luck.

Upvotes: 1

Views: 203

Answers (1)

timolawl
timolawl

Reputation: 5564

Only going off of what you have provided, I have found this to work:

^(?:(?:http|https):\/\/)?(?:www.)?facebook.com\/(?:(?:\w)*#!\/)?(?:pages\/)?(?:[?\w\-]*\/)?(?:profile.php\?id=(?=\d.*))?([\w\-\.]*)?(?:\?fref=ufi)?$

The difference is in:

([\w\-\.]*)? // added the \. for matching the . literally
(?:\?fref=ufi)?$ // this was appended to the end

Regex101

This may not work for all scenarios as I only have your test case to work off of.

Upvotes: 1

Related Questions