[英]How to extract a file name from an URI in javascript?
I have an URL like this -我有一个像这样的 URL -
https://firebasestorage.googleapis.com/v0/b/sathyatest12-blog.appspot.com/o/85fa93fa-1ca7-4055-b6b3-6a41db4s498b-testcup1.png?alt=media&token=4dfd3ca0-a55b-4052-956c-94a2bs13c77e
of which I need to get the file name.其中我需要获取文件名。 In this case,
在这种情况下,
85fd93fa-1ca7-4055-b6d3-6a41db4s498b-testcup1.png
85fd93fa-1ca7-4055-b6d3-6a41db4s498b-testcup1.png
I tried using regex as suggested in other answers but I am unable to write complicated expressions.我尝试按照其他答案中的建议使用正则表达式,但我无法编写复杂的表达式。
I tried using JavaScripts Good parts by Coderholic but it is not giving me what I want.我尝试使用Coderholic 的 JavaScripts Good 部分,但它并没有给我想要的东西。 I found a generic regex from there which I am pasting below
我从那里找到了一个通用的正则表达式,我将其粘贴在下面
var parse_url = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/; var url = 'http://www.ora.com:80/goodparts?q#fragment'; var result = parse_url.exec(url); var names = ['url', 'scheme', 'slash', 'host', 'port', 'path', 'query', 'hash']; var blanks = ' '; var i; for (i = 0; i < names.length; i += 1) { document.writeln(names[i] + ':' + blanks.substring(names[i].length), result[i]); }
But this is giving me但这给了我
/v0/b/sathyatest12-blog.appspot.com/o/85fa93fa-1ca7-4055-b6b3-6a41db4s498b-testcup1.png
/v0/b/sathyatest12-blog.appspot.com/o/85fa93fa-1ca7-4055-b6b3-6a41db4s498b-testcup1.png
Please help.请帮忙。
Why not simply use the URL API ?为什么不简单地使用URL API呢?
var URL_val = 'https://firebasestorage.googleapis.com/v0/b/sathyatest12-blog.appspot.com/o/85fa93fa-1ca7-4055-b6b3-6a41db4s498b-testcup1.png?alt=media&token=4dfd3ca0-a55b-4052-956c-94a2bs13c77e', URL_Obj = new URL(URL_val); let lastOne = URL_Obj.pathname.split('/').pop() console.log(`lastOne: ${lastOne}`)
I have solved this problem simply like below我已经解决了这个问题,如下所示
var url_string = "https://firebasestorage.googleapis.com/v0/b/sathyatest12-blog.appspot.com/o/85fa93fa-1ca7-4055-b6b3-6a41db4s498b-testcup1.png?alt=media&token=4dfd3ca0-a55b-4052-956c-94a2bs13c77e"; //window.location.href
firstPos = url_string.lastIndexOf('/');
lastPos = url_string.indexOf('?');
var result = url_string.slice(firstPos+1, lastPos);
console.log(result);
I have tested this on jsfiddle.net我已经在 jsfiddle.net 上对此进行了测试
You can also achieve the same result first to reverse the string since we cannot find the last index of /
using regex.您也可以首先获得相同的结果来反转字符串,因为我们无法使用正则表达式找到
/
的最后一个索引。 But we can find the first index of ?
但是我们可以找到 的第一个索引
?
if we can reverse the string.如果我们可以反转字符串。
const str = "https://firebasestorage.googleapis.com/v0/b/sathyatest12-blog.appspot.com/o/85fa93fa-1ca7-4055-b6b3-6a41db4s498b-testcup1.png?alt=media&token=4dfd3ca0-a55b-4052-956c-94a2bs13c77e"; const reverse = str.split("").reverse().join(""); const regex = /(?<=\?)(.*?)\//; const match = reverse.match(regex); if (match) { const result = match[1].split("").reverse().join(""); console.log(result); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.