簡體   English   中英

返回給定 URL 中音頻文件的最后一個文件夾名稱

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

我想返回 wav 文件所在的最后一個文件夾:

所以如果我們有這個 URL:

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

我們應該准確返回:

你喜歡你的新冷凍層嗎

我可以 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);

我們應該怎么做?

將我的評論轉換為答案,以便將來的訪問者輕松找到該解決方案。

您可以像這樣進行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

在最后一個反斜杠之前獲取 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