Alex Rivera | Logout

Regex to match the URL last part with JavaScript

Asked 2012-02-29T07:46:58.440
11

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?

Edit
Report

1 Answer

26

Take a look at the lastIndexOf method:

var index = url.lastIndexOf("/");
var fileName = url.substr(index)
answered 2012-02-29T07:49:03.553

Your Answer