0

I would like to remove "/first" string and remove _1_1_1 at the end of the url.

/foo/blah/tree/first/image_pack-1-1-1

what i need to turn is

/foo/blah/tree/image_pack

Here's what I have done but doesn't work as i wish. I'm stuck at some point.

var fileUrl = "/foo/blah/tree/first/image_pack-1-1-1";
var output = fileUrl.split(/[/ ]+/).pop();
var explode = output.split("-");
console.log(explode[0]);
13
  • 1
    Seriously, its just fileUrl.split('-')[0]... Commented Aug 11, 2015 at 14:38
  • 3
    No, it's not. Read the question again. Commented Aug 11, 2015 at 14:39
  • is /first/ a constant? will it ever be /second/? Commented Aug 11, 2015 at 14:40
  • 2
    Will /first and -1-1-1 always be the same? Commented Aug 11, 2015 at 14:40
  • 1
    @somethinghere come on now. No fighting. Commented Aug 11, 2015 at 14:41

3 Answers 3

1

Not sure you can realistically get there in one line. How about this. It splits against the first dash, splits the first element against the backslashes, and then splices that 4th element right out regardless of what it's called. Then just join it back together.

var output = fileUrl.split('-')[0].split('/');
output.splice(4, 1);
output = output.join('/'); // /foo/blah/tree/image_pack

DEMO

Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much @Andy, This worked out of box. Best Regards
1

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

3 Comments

thanks for the answer but first changes all the time, and the -1-1-1 number is changing but their place is always same. @somethinghere
@Profstyle which is why I wrote you a function to allows first to change all the time. Just fill in the correct string and it will remove anything you want to remove.
thanks @somethinghere but Andy's solution worked better for me, Thanks for your help.
0

would something like this work?

var fileUrl = "/foo/blah/tree/SOMERANDOMTHINGREMOVEDHERE/image_pack-1-1-1";
var newUrl1 = fileUrl.split('-')[0].split('/');
console.log(newUrl1);
var finalUrl = '';
for(var i = 0; i < newUrl1.length; i++) {
    if (i != 4) {
        finalUrl += newUrl1[i] + '/';
    }
}
alert(finalUrl);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.