我的文件路径为
falsefile:/// var / mobile / Containers / Data / Application / D4B6F6CD-5E5C-4459-90CC-0C649B3B31B8 / Documents / ExponentExperienceData /%2540hherax%252Fiia-mas-app-new // IIAMASATTCHMENTS / BD6FE729-70F1- 48B0-83EB-8E7D956E599E.MOV
因为文件扩展名将随着文件类型而变化 文件路径也会更改
我该如何操作字符串以获取文件名
BD6FE729-70F1-48B0-83EB-8E7D956E599E"
在给定的示例中
路径和文件类型更改的第二个示例
falsefile:/// var / mobile / Containers / Data / Application / D4B6F6CD-5E5C-4459-90CC-0C649B3B31B8 / Documents / ExponentExperienceData /%2540ppphrx%252Fiia-mas-app-new // IIAMASATTCHMENTS / DD6FE729-60F2- 58B0-8M8B-8E759R6E547K.jpeg
答案 0 :(得分:3)
您可以轻松完成
let str="falsefile:///var/mobile/Containers/Data/Application/D4B6F6CD-5E5C-4459-90CC-0C649B3B31B8/Documents/ExponentExperienceData/%2540hherax%252Fiia-mas-app-new//IIAMASATTCHMENTS/BD6FE729-70F1-48B0-83EB-8E7D956E599E.MOV"
console.log( str.split(".")[0].split("/").pop()
)
只记得拆分拆分弹出
答案 1 :(得分:0)
切片/分割的某些变化会起作用
const str = 'falsefile:///var/mobile/Containers/Data/Application/D4B6F6CD-5E5C-4459-90CC-0C649B3B31B8/Documents/ExponentExperienceData/%2540hherax%252Fiia-mas-app-new//IIAMASATTCHMENTS/BD6FE729-70F1-48B0-83EB-8E7D956E599E.MOV'
console.log(
str.slice(str.lastIndexOf("/")+1).split(".")[0]
)
// or
console.log(
str.split("/").pop().split(".")[0]
)
答案 2 :(得分:0)
例如,您可以使用正则表达式。 我想到的第一件事是:
const filepath = 'falsefile:///var/mobile/Containers/Data/Application/D4B6F6CD-5E5C-4459-90CC-0C649B3B31B8/Documents/ExponentExperienceData/%2540hherax%252Fiia-mas-app-new//IIAMASATTCHMENTS/BD6FE729-70F1-48B0-83EB-8E7D956E599E.MOV'
const filenameWithoutExtension = filepath.match(/IIAMASATTCHMENTS\/(.*)\./)[1] // "BD6FE729-70F1-48B0-83EB-8E7D956E599E"
console.log(filenameWithoutExtension)
答案 3 :(得分:0)
您可以使用如下所示的正则表达式:
function get_filename(str) {
const regex = /\/([A-Z0-9\-_]+)\.[\w\d]+/gm;
let m = regex.exec(str);
return m[1];
}
console.log(
get_filename(`falsefile:///var/mobile/Containers/Data/Application/D4B6F6CD-5E5C-4459-90CC-0C649B3B31B8/Documents/ExponentExperienceData/%2540ppphrx%252Fiia-mas-app-new//IIAMASATTCHMENTS/DD6FE729-60F2-58B0-8M8B-8E759R6E547K.jpeg`)
)
答案 4 :(得分:0)
var str = "falsefile:///var/mobile/Containers/Data/Application/D4B6F6CD-5E5C-4459-90CC-0C649B3B31B8/Documents/ExponentExperienceData/%2540hherax%252Fiia-mas-app-new//IIAMASATTCHMENTS/BD6FE729-70F1-48B0-83EB-8E7D956E599E.MOV",
re = /[\w|-]*\.\w*/
stringNameWithExt = str.match(re)
stringNameWithoutExt = str.match(re)[0].split(".")[0]
console.log(stringNameWithoutExt)
答案 5 :(得分:0)
var filpath = "falsefile:///var/mobile/Containers/Data/Application/D4B6F6CD-5E5C-4459-90CC-0C649B3B31B8/Documents/ExponentExperienceData/%2540hherax%252Fiia-mas-app-new//IIAMASATTCHMENTS/BD6FE729-70F1-48B0-83EB-8E7D956E599E.MOV"
console.log(
filpath.substring(filpath.lastIndexOf('/') + 1, filpath.length).substring(1, filpath.substring(filpath.lastIndexOf('/') + 1, filpath.length).lastIndexOf('.'))
)
答案 6 :(得分:0)
如果知道要捕获的值的格式,则可以使用正则表达式获得更精确的匹配,然后在第一个捕获组中捕获值。
您可以使用/i
标志使比赛区分大小写。
([A-Z0-9]+(?:-[A-Z0-9]+){4})\.\w+$
这将匹配:
(
捕获组
[A-Z0-9]+
匹配1次以上character class中列出的内容(?:-[A-Z0-9]+){4}
重复4次匹配连字符和1+倍字符类中列出的内容)
关闭捕获组\.\w+$
匹配一个点,一个字符char超过1倍,并断言字符串的结尾
let strs = [
`falsefile:///var/mobile/Containers/Data/Application/D4B6F6CD-5E5C-4459-90CC-0C649B3B31B8/Documents/ExponentExperienceData/%2540hherax%252Fiia-mas-app-new//IIAMASATTCHMENTS/BD6FE729-70F1-48B0-83EB-8E7D956E599E.MOV`,
`falsefile:///var/mobile/Containers/Data/Application/D4B6F6CD-5E5C-4459-90CC-0C649B3B31B8/Documents/ExponentExperienceData/%2540ppphrx%252Fiia-mas-app-new//IIAMASATTCHMENTS/DD6FE729-60F2-58B0-8M8B-8E759R6E547K.jpeg`
];
let pattern = /([A-Z0-9]+(?:-[A-Z0-9]+){4})\.\w+$/i;
strs.forEach(str => console.log(str.match(pattern)[1]));