Say I have the following:
<img src="http://www.site.com/folder/pic.jpg">
This path could be anything, we basically want to get the "pic.jpg as a variable.
Currently we are doing it like so:
var first_img = $("#thumbnail-area li:first").find("img").attr("title");
Which sets the first_img
variable as the image src, but we want to do a preg match kinda thing like in PHP to grab the "pic.jpg".
This has to work properly, so the path could be: folder/foo/bar/x982j/second822.jpg
and it'd return second822.jpg
How can I do this?
You could use replace()
which is like PHP's preg_replace()
(it too accepts a PCRE, with some limitations such as no look behinds)...
str.replace(/.*\//, '')
jsFiddle.
Alternatively, you could use...
str.split('/').pop();
jsFiddle.
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