简体   繁体   English

返回给定 URL 中音频文件的最后一个文件夹名称

[英]Return the last folder name of the audio file in the given URL

I want to return the the last folder in which the wav file is inside:我想返回 wav 文件所在的最后一个文件夹:

So if we have this URL:所以如果我们有这个 URL:

const dec = "https://langfox.ir/expect/sources/frozen_2019/the_beautiful_forest_version_1/are you enjoying your new frozen layer/1614282551749614446037f2253780f2f.wav";

We should return exactly:我们应该准确返回:

are you enjoying your new frozen layer你喜欢你的新冷冻层吗

I can substring the url but I can't find out the index of / signs correctly...我可以 substring url 但我无法正确找到 / 标志的索引...

 const dec = "https://langfox.ir/expect/sources/frozen_2019/the_beautiful_forest_version_1/are you enjoying your new frozen layer/1614282551749614446037f2253780f2f.wav" var mySubString = dec.substring( dec.lastIndexOf("/") - X, // X is something I can't find out dec.lastIndexOf("/") ); console.log(mySubString);

How can we do this?我们应该怎么做?

Converting my comment to answer so that solution is easy to find for future visitors.将我的评论转换为答案,以便将来的访问者轻松找到该解决方案。

You can do it split + splice operation like this:您可以像这样进行split + splice操作:

 const dec = "https://langfox.ir/expect/sources/frozen_2019/the_beautiful_forest_version_1/are you enjoying your new frozen layer/1614282551749614446037f2253780f2f.wav"; var p = dec.split('/').slice(-2)[0]; console.log(p); //=> are you enjoying your new frozen layer

Get the index of the backslash in a substring before the last backslash:在最后一个反斜杠之前获取 substring 中反斜杠的索引:

 const dec = "https://langfox.ir/expect/sources/frozen_2019/the_beautiful_forest_version_1/are you enjoying your new frozen layer/1614282551749614446037f2253780f2f.wav" var mySubString = dec.substring( dec.substring(0, dec.lastIndexOf("/")).lastIndexOf("/")+1, dec.lastIndexOf("/") ); console.log(mySubString);

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

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