xRobot xRobot - 5 years ago 84
Javascript Question

How to get the ID of a page from the url?

I need to get this: 1606321809619553

From that: https://www.facebook.com/Free-Travel-Vacations-1606321809619553/?ref=br_rs

This: barackobama

From that: https://www.facebook.com/barackobama/

and This: barackobama

From that: https://www.facebook.com/barackobama

I tried this code:

identifier = url.substring(url.lastIndexOf("/"))

but it only work when there is not the final slash url ( like in the last case above ).

Answer Source

you can leverage regexp

var re = /^https:\/\/www\.facebook\.com\/([-\w]+)/;

=> "Free-Travel-Vacations-1606321809619553" //then use string manipulation

=> "barackobama"

=> "barackobama"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download