Detale Detale - 1 year ago 65
Javascript Question

Finding a numeric ID within an URL including numbers

I've run into some problems accessing and forwarding an ID.
I successfully extracted the ID from my URL but I run into problems if the URL contains unpredictable numbers aswell.

To clear things up a bit:

My efforts to extract the ID so far (JS)

var idString = window.location.href;
idString = idString.replace(/e107/gi, "__ersetzt__");
idString = idString.replace("http://localhost/Westbomke/backendV5/", "");
idString = idString.replace(/[^0-9]+/g, "");

Some URL examples


http://localhost/Westbomke/backendV5/e107-master/e107_projekte/BMW/235_Projekt_BMW-Event/page.php    Result: 235 = id

Not working:



now if I could exclude the   /******_Projekt_    Part (**** = random amount of numbers) and parse it into an Integer I would be fine, but I dont know how to do this or if it's possible.

I tried to find something on here and via google but I most likely dont ask for the right stuff.

Thanks for your time and help in advance!

hsz hsz
Answer Source

You can try with:

var url = 'http://localhost/Westbomke/backendV5/e107-master/e107_projekte/BMW/235_Projekt_BMW-Event/page.php';
var id = +url.match(/\/(\d+)/)[1];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download