I want a JavaScript regex to extract the value of an id from facebook profile URL. For example, I have a url
https://www.facebook.com/profile.php?id=100004774025067
and I just want to extract the value after the id=
which is the number 100004774025067
part only.
But In some cases I will need the user profile url from a post. For example a user posted something, and if i get the profile url link of the post then I get the link like this:
https://www.facebook.com/profile.php?id=100001883994837&hc_ref=ART7eWRecFS8mMIio66GdaH378zlJMXzisnKubh5PtgINeVwTfOil5aBIyff71OamWA
As you can see, after the value of id
there's an additional parameter specified.
I only need to get the id
value, no matter what else is in the link.
Answer
You can use the string as a URL and extract the id
parameter using searchParams
:
Without additional parameters:
var fb_url = "https://www.facebook.com/profile.php?id=100004774025067";
var url = new URL(fb_url);
var uid = url.searchParams.get("id");
console.log(uid);
With more parameters:
var fb_url = "https://www.facebook.com/profile.php?id=100004774025067&hc_ref=ART7eW";
var url = new URL(fb_url);
var uid = url.searchParams.get("id");
console.log(uid);
No comments:
Post a Comment