1

Not sure how I would go about writing a formula to do this... I need to do:

current_url = "http://something.random.com/something/something/upload/something/something_else"
new_url = "http://something.random.com/something/something/upload/{NEWSOMETHING}/something/something_else"

Basically I'm always trying to insert another string segment exactly after the upload/ in the original URL. I've thought about using position but I don't have a fixed position because current_url won't always be the same length. The only constant is that I know the string needs to be inserted after upload/, wherever it may be

3 Answers 3

7
current_url.replace("upload/","upload/{NEWSOMETHING}/")

If your string is var current_url = "http://something.random.com/something/something/upload/something/c_fit/something_else/c_fit/"; and you want to replace everything in between upload and the last c_fit then current_url.replace(/\/upload\/.*\/c_fit\//,"/upload/"+"<YOUR_STRING>"+"/c_fit/") but you just want to replace between upload and the first c_fit then current_url.replace(/\/upload\/.*?\/c_fit\//,"/upload/"+"<YOUR_STRING>"+"/c_fit/")

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

2 Comments

That's a BRILLIANT idea... It never occurred to me because I was so fixated on not changing upload!
actually @vcosk, it turns out the code is not always that simplistic. I need to replace "upload/[any text whatever up to]c_fit". Basically, the text between upload/ and c_fit is what's getting replaced, but the text between those is varied in length, characters, and etc. Do you know of a way to do this? Happy to make a second question, Just figured I'd ask in case you had another brilliant idea!
0
var current_url = "http://something.random.com/something/something/upload/something/something_else";

var chunks = current_url.split("/");

var str = [];

var s = chunks.shift();
while(s != "upload"){
    str.push(s);
    s = chunks.shift();
}

var new_url = str.join('/')+"/upload/{new something}/something/something_else";

alert(new_url);

Comments

0

You could easily split the string on the static text "upload".

var current_url = "http://something.random.com/something/something/upload/something/something_else";
    var splitArray = current_url.split("upload");
    var additionalParameter = "What_ever_comes_after_upload"
    var new_url = splitArray[0] + "upload/" + additionalParameter + splitArray[1];

alert(new_url);

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.