Delete full path, save only file name

Trying to remove the full URL that returns to imgurl: Usually returns something like http: //localhost/wordpress/wp-content/uploads/filename.jpg or http: // localhost / wordpress / wp-content / uploads / images / filename.jpg

I would like to delete everything except filename.jpg and return it ahng_photos_upload_image. Reset everything to the last slash. How can I do this using jQuery?

window.send_to_editor = function(html) {
imgurl = jQuery('img',html).attr('src');
jQuery('#ahng_photos_upload_image').val(imgurl);
tb_remove();
}
+5
source share
8 answers

You do not need jQuery for this, just the old JavaScript will do :)

alert('http://localhost/wordpress/wp-content/uploads/filename.jpg'.split('/').pop());​​

In your case:

var filename = imgurl.split('/').pop();
+22
source

You can use regex to achieve this.

var file = imgUrl.replace(/^.*[\\\/]/, '');

.

+2

, URL- , , :

var filename = imgurl.replace(/^.*\/([^/]*)$/, "$1");

: "imgurl" var, , , .prop() .attr(), jQuery 1.6 :

var imgurl = jQuery('img', html).prop('src');

jQuery :

var imgurl = jQuery(html).find('img').prop('src');

.

+1

:

var filename = imgurl.substring(imgurl.lastIndexOf('/') + 1);

JS Fiddle demo.

+1

:

imgurl.split('/').slice(-1);

: @Andy, pop(), , slice(-1).

0

var filename = imgurl.split('/').slice(-1);

!

0

Note that if you do not know if you have fast forward or rewind, you are better off using the RE split version:

"path".split(/[\/\\]/).slice(-1)
0
source

Here is an answer that will work when your file name looks like. /file.jpg

var extension = fileName.slice((fileName.lastIndexOf(".") - 1 >>> 0) + 2);
var baseName = fileName.replace(/^.*\/([^/]*)$/, "$1");
var path = fileName.replace(/(^.*\/)([^/]*)$/, "$1");
0
source

All Articles