Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

So if we have this 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 ...

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?

like image 530
Sara Ree Avatar asked Nov 24 '25 11:11

Sara Ree


2 Answers

Converting my comment to answer so that solution is easy to find for future visitors.

You can do it split + splice operation like this:

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
like image 65
anubhava Avatar answered Nov 26 '25 02:11

anubhava


Get the index of the backslash in a substring before the last backslash:

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);
like image 38
Ryszard Czech Avatar answered Nov 26 '25 00:11

Ryszard Czech



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!