I have some URLs and I like to catch the final part of the url.
My URLs are in the form of
http://www.my-site.dch/wp-content/uploads/2012/02/Tulips.jpg
http://www.my-site.dch/wp-content/uploads/2012/02/Tulips-150x200.jpg
http://www.my-site.dch/wp-content/uploads/2012/02/Tulips-500x350.jpg
and what I like to catch is the /Tulips.......jpg
I have try that but with no luck
\/.*(-\d+x\d+)\.(jp(e)?g|png|gif)
Any better idea?
In case you came here looking to find the last part of the url even if the url ends with /
There's a solution which is simpler than any regex solution. Even though the question is regarding regex, I will add it because it does add some value here.
If the url was this http://www.my-site.dch/wp-content/uploads/2012/02/Tulips.jpg
const url = 'http://www.my-site.dch/wp-content/uploads/2012/02/Tulips.jpg'
const lastpart = url.split('/').filter(e => e).pop()
In this case, the last part would return the last part even if it ends with /
So, if you had a url like this
/services/mosquito-control/
and you wanted to catch mosquito-control
, you would be do it with this.
The following regular expression will work:
/[^\/]+$/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With