I am not too good with Regular Expressions in Javascript. Does anyone know an efficient way to capture the last portion of a URL???
I have the following URL:
http://localhost:3000/developers/568d3c3c82eea6e6fb47c236
And all I need to do is capture the developer ID (which is 568d3c3c82eea6e6fb47c236). This route will always be the same (with just the ID's changing).
Any help would be much appreciated
You can use
split
by/
and get the last element of srray:You don't need a regular expression; just use
lastIndexOf
method:You can use following code snippet
There are built in methods for this:
This will get everything after the domain name (
window.location.pathname
), then split it by forward slashes (split("/")
), then return the last item of the array returned bysplit()
, (pop()
).