Sara Ree
Sara Ree

Reputation: 3543

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?

Upvotes: 3

Views: 36

Answers (2)

anubhava
anubhava

Reputation: 785276

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

Upvotes: 2

Ryszard Czech
Ryszard Czech

Reputation: 18621

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);

Upvotes: 1

Related Questions