The question on everyone's mind is: how recurring is this? Is first a variable? Is 1-1-1-1 always at the end? If both of them are constant, then it's easy:
fileUrl.split('-')[0].replace('first/','')
But otherwise, you might be better off constructing a function for it:
function fileUrl(url, replacements){
// Allow for replacements to be empty
replacements = replacements || [];
// Split at the `-` - a more versatile way would be too complex here.
url = url.split('-')[0];
for(var i = 0; i < replacements.length; i++){
url = url.replace(replacements[i], '');
}
// Remove any double slashes that have accumulated
while(url.indexOf('//').length >= 0) url.replace('//','/');
return url;
}
Now you can easily do this:
fileUrl('/foo/blah/tree/first/image_pack-1-1-1', ['first']);
function fileUrl(url, replacements){
replacements = replacements || [];
url = url.split('-')[0];
for(var i = 0; i < replacements.length; i++){
url = url.replace(replacements[i], '');
}
while(url.indexOf('//').length >= 0) url.replace('//','/');
return url;
}
document.write(fileUrl('/foo/blah/tree/first/image_pack-1-1-1', ['first']));
fileUrl.split('-')[0].../firstand-1-1-1always be the same?