Say I have the following:
This path could be anything, we basically want to get the
Working example here: http://jsfiddle.net/jkeyes/sxx3T/
var re = new RegExp(".*\/(.*)$");
var src="folder/foo/bar/x982j/second822.jpg";
var m = re.exec(src);
alert(m[1]); // first group
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.
jQuery is not necessary here; Javascript supports regular expressions on its own, so jQuery is not part of the answer.
Javascript's regex replace function is simply called .replace()
, and is a method on the string class. You would use it as follows:
var mystring = 'this is a string';
mystring.replace(/is a/,'might be a');
//mystring is now equal to 'this might be a string'.
That should be enough to get you started. Since you referenced preg_replace()
in the question, I assume you already know how to use regular expressions well enough not to need a detailed discussion of how to solve your specific example.