Given /foo/bar/image.jpg?x=1&y=2, how do I obtain image.jpg?
https://stackoverflow.com/a/423385 provides a partial answer, but does not address the GET parameters.
Given /foo/bar/image.jpg?x=1&y=2, how do I obtain image.jpg?
https://stackoverflow.com/a/423385 provides a partial answer, but does not address the GET parameters.
You can use regex as others have suggested, but I find this more readable:
var src = '/foo/bar/image.jpg?x=1&y=2';
var img = src.split('/').pop().split('?')[0];
console.log(img);
You can use this regex:
/\/([^?\/]+(?=\?|$))/
and use captured grpup #1.
/ # matches a literal /
[^?\/]+ # matches 1 or more of any char that is not ? or /
(?=\?|$) # is a lookahead to assert that next position is ? or end of line
/ in captured group. I wrote clearly use captured grpup #1 in my answer.
/foo/bar/image.jpg?x=1&y=2is thesrcof an image, and not the page URL. Does your recommendation still apply?