javascript 删除完整路径,只保留文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9353315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:22:05  来源:igfitidea点击:

remove full path, keep filename only

javascriptjquerywordpress

提问by user1219691

Trying to remove the full url that is being returned to imgurl: Usually returns something like http://localhost/wordpress/wp-content/uploads/filename.jpgor http://localhost/wordpress/wp-content/uploads/images/filename.jpg

试图删除返回到 imgurl 的完整 url:通常返回类似http://localhost/wordpress/wp-content/uploads/filename.jpghttp://localhost/wordpress/wp-content/uploads/images 的内容/文件名.jpg

I'd like to strip off everything except filename.jpg and return it to ahng_photos_upload_image. Strip off everything to the last forward-slash. How can I do that with Jquery?

我想剥离除 filename.jpg 之外的所有内容并将其返回给 ahng_photos_upload_image。将所有内容剥离到最后一个正斜杠。我怎样才能用 Jquery 做到这一点?

window.send_to_editor = function(html) {
imgurl = jQuery('img',html).attr('src');
jQuery('#ahng_photos_upload_image').val(imgurl);
tb_remove();
}

回答by Andy

You don't need jQuery for that, just plain old JavaScript will do :)

你不需要 jQuery,只需要普通的旧 JavaScript 就可以了 :)

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

In your case:

在你的情况下:

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

回答by Vivek Chandra

you can use a regular expression in order to achieve this..

您可以使用正则表达式来实现这一点..

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

Now the file would consist of only the file name ..

现在文件将只包含文件名 ..

回答by Pointy

If you're pretty confident that the URLs don't have funny stuff like hashes or parameters, a regex like this would do it:

如果您非常确信 URL 没有诸如哈希或参数之类的有趣内容,则可以使用这样的正则表达式:

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

Also: don't forget to declare "imgurl" with var, and you should probablyuse .prop()instead of .attr()if your version of jQuery is 1.6 or newer:

另外:不要忘记用 声明“imgurl” var,如果您的 jQuery 版本是 1.6 或更高版本,您可能应该使用.prop()而不是.attr()

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

Also jQuery internally turns the two-argument form of the function into this:

此外,jQuery 在内部将函数的两个参数形式转换为:

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

so you might as well code it that way.

所以你也可以这样编码。

回答by David says reinstate Monica

One further option:

另一种选择:

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

JS Fiddle demo.

JS小提琴演示

回答by sdw

Note that if you don't know if you have forward or backward slashes, you are better off using the RE version of split:

请注意,如果您不知道是否有正斜杠或反斜杠,最好使用 RE 版本的 split:

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

回答by Ari

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

这是一个当您的文件名类似于 ./file.jpg 时有效的答案

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

回答by piouPiouM

Try this one:

试试这个:

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

Edit:Look at the version of @Andywho uses the pop()method, the latter being faster than slice(-1).

编辑:查看使用该方法的@Andy 的版本pop(),后者比slice(-1).

回答by Luis

Here you have

在这里你有

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

Good luck!

祝你好运!