Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent in jQuery to PHP's `preg_replace()`?

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?

like image 559
Latox Avatar asked Oct 06 '11 11:10

Latox


1 Answers

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.

like image 137
alex Avatar answered Oct 08 '22 11:10

alex