I have a variable which holds a relative link for an image. This image link file name can have various lengths. For example…
/v/vspfiles/assets/images/d-amazingamethyst.jpg
/v/vspfiles/assets/images/cdacarbon.jpg
I would like to extract from this variable only the filename with out the .jpg extention and without the preceeding path. So the first example above should return d-amazingamethyst
A Jquery answer is preferred but straight javascript is ok as well. Thanks
Methods on jsFiddle
This returns “file-only.my”
It is going to get the substring, starting from the index of the last
/plus 1 (the beggining of the filename) until the last index of the., cutting the extension.You can also use the
splitfunction.But then you need to handle
.s on the filename, if you cut by the index 0 it will get the wrong filename.I use
.popto remove the last element on the array, which is going to be the file extension, then I join everything back with.s.