The initial string:
initString = '/digital/collection/music/bunch/of/other/stuff'
What I want: music
- Specifically, I want any term (will never include slashes) that would come between
collection/
and/bunch
How I'm going about it:
if(initString.includes('/digital/collection/')){
let slicedString = initString.slice(19); //results in 'music/bunch/of/other/stuff'
let indexOfSlash = slicedString.indexOf('/'); //results, in this case, to 5
let desiredString = slicedString.slice(0, indexOfSlash); //results in 'music'
}
Question:
How the heck do I accomplish this in javascript in a more elegant way?
- I looked for something like an
endIndexOf()
that would replace my hardcoded.slice(19)
lastIndexOf()
isn't what I'm looking for, because I want the index at the end of the first instance of my substring/digital/collection/
- I'm looking to keep the number of lines down, and I couldn't find anything like a
.getStringBetween('beginCutoff, endCutoff')
Thank you in advance!