简体   繁体   English

在 JavaScript 中,如何从长字符串中提取图像文件的名称?

[英]In JavaScript, how can I extract the name of an image file from a long string?

Can you help to extract the file name from this string?你能帮忙从这个字符串中提取文件名吗?

let str = "https://s3.amazonaws.com/test/testSite/product/image_folder/Z1-00034-01/1623934721778.jpeg%25253FX-Amz-Algorithm%25253DAWS4-HMAC-SHA256%252526X-Amz-Date%25253D20210722T050758Z%252526X-Amz-SignedHeaders%25253Dhost%252526X-Amz-Expires%25253D599%252526X-Amz-Credential%25253DAKIA4Z2VUPBRCPYFDP4G%2525252F20210722%2525252Fus-east-1%2525252Fs3%2525252Faws4_request%252526X-Amz-Signature%25253D259df0ed0fd8f827e74c612ff8d87e65e2f2c8d68f75a2f06eb71a84de3847a1%253FX-Amz-Algorithm%253DAWS4-HMAC-SHA256%2526X";

Output should be 1623934721778.jpeg .输出应该是1623934721778.jpeg

Assuming you always wanted to target a JPEG file, you could use match here:假设你一直想定位一个 JPEG 文件,你可以在这里使用match

 let str = "https://s3.amazonaws.com/test/testSite/product/image_folder/Z1-00034-01/1623934721778.jpeg%25253FX-Amz-Algorithm%25253DAWS4-HMAC-SHA256%252526X-Amz-Date%25253D20210722T050758Z%252526X-Amz-SignedHeaders%25253Dhost%252526X-Amz-Expires%25253D599%252526X-Amz-Credential%25253DAKIA4Z2VUPBRCPYFDP4G%2525252F20210722%2525252Fus-east-1%2525252Fs3%2525252Faws4_request%252526X-Amz-Signature%25253D259df0ed0fd8f827e74c612ff8d87e65e2f2c8d68f75a2f06eb71a84de3847a1%253FX-Amz-Algorithm%253DAWS4-HMAC-SHA256%2526X"; filename = str.match(/\\b[^/\\s]+\\.(?:jpeg|png)/)[0]; console.log(filename);

Not sure why you're getting a triple escaped URL, but you can fix that like this:不知道为什么你会得到一个三重转义的 URL,但你可以像这样修复它:

 let str = "https://s3.amazonaws.com/test/testSite/product/image_folder/Z1-00034-01/1623934721778.jpeg%25253FX-Amz-Algorithm%25253DAWS4-HMAC-SHA256%252526X-Amz-Date%25253D20210722T050758Z%252526X-Amz-SignedHeaders%25253Dhost%252526X-Amz-Expires%25253D599%252526X-Amz-Credential%25253DAKIA4Z2VUPBRCPYFDP4G%2525252F20210722%2525252Fus-east-1%2525252Fs3%2525252Faws4_request%252526X-Amz-Signature%25253D259df0ed0fd8f827e74c612ff8d87e65e2f2c8d68f75a2f06eb71a84de3847a1%253FX-Amz-Algorithm%253DAWS4-HMAC-SHA256%2526X"; const url = new URL(unescape(unescape(unescape(str)))); const filename = url.pathname.split('/').pop(); console.log(filename)

 let str = "https://s3.amazonaws.com/test/testSite/product/image_folder/Z1-00034-01/1623934721778.jpeg%25253FX-Amz-Algorithm%25253DAWS4-HMAC-SHA256%252526X-Amz-Date%25253D20210722T050758Z%252526X-Amz-SignedHeaders%25253Dhost%252526X-Amz-Expires%25253D599%252526X-Amz-Credential%25253DAKIA4Z2VUPBRCPYFDP4G%2525252F20210722%2525252Fus-east-1%2525252Fs3%2525252Faws4_request%252526X-Amz-Signature%25253D259df0ed0fd8f827e74c612ff8d87e65e2f2c8d68f75a2f06eb71a84de3847a1%253FX-Amz-Algorithm%253DAWS4-HMAC-SHA256%2526X"; var patt1 = /[\\w&.-]+\\.([0-9a-z]+)(?:[\\?#%]|$)/i; var matches= str.match(patt1); let filename = matches[0].split(".")[0] + '.' +matches[1] console.log(matches) console.log(filename) //EDIT var patt2 = /([\\w&.-]+)\\.([0-9a-z]+)(?:[\\?#%]|$)/i; var matches= str.match(patt2); let filename2 = matches[1] + '.' +matches[2] console.log(matches) console.log(filename2)

  1. [\\w&.-] - Matches FileName. [\\w&.-] - 匹配文件名。 If you change [\\w&.-] to [/\\/\\w&.-] it will match all characters except https://如果将[\\w&.-] to [/\\/\\w&.-]更改[\\w&.-] to [/\\/\\w&.-]它将匹配除 https:// 之外的所有字符
  2. [0-9a-z] - This one matches extension [0-9a-z] - 这个匹配扩展名
  3. [\\?#%] - Here you can add special characters to be removed [\\?#%] - 这里可以添加要删除的特殊字符
  4. $ symbol will match last words $ 符号将匹配最后的话
  5. /i - ignore case in the given string /i - 忽略给定字符串中的大小写
  6. ([\\w&.-]+) and ([0-9a-z]+) will match filename and extension seperate ([\\w&.-]+) and ([0-9a-z]+)将分别匹配文件名和扩展名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM