0

I have a URL that always ends with the number 30. I want to replace that with another number 100. I am not sure if regex is the best option and if so, how to do it? This is my attempt, but I am looking for something more professional that would work in any situation and only change the last bit of the URL that has 30.

let URL = "https://is5-ssl.mzstatic.com/image/thumb/Music/v4/5c/5a/a0/5c5aa0fd-978e-b038-3918-f6127f97295e/source/30x30bb.jpg";
URL.replace("/([30])\w+/g", "100");

Thanks in advance.

6
  • 2
    URL = URL.replace('30x30', '100x100') ? Commented Jul 25, 2019 at 20:16
  • 2
    @charlietfl has a great idea. I might tweak it a little like this: URL.replace('/30x30', '/100x100') just to decrease the risk of "30x30" showing up elsewhere, such as in the string that looks like a hash. Commented Jul 25, 2019 at 20:19
  • 1
    @charlietfl I have a URL that always ends with the number 30. I want to replace that with another number 100. i guess op is talking about only last one not both Commented Jul 25, 2019 at 20:23
  • Actually I am talking about both. My bad. I just dont want it to be replaced if 30 is somweher wlese in the URL like http://somewebsite30.com/30x30.jpg Commented Jul 25, 2019 at 20:24
  • 1
    @DannyBoy what if the url is something like http://somewebsite30.com/40x30.jpg or http://somewebsite30.com/30x40.jpg ? Commented Jul 25, 2019 at 20:27

3 Answers 3

1

You could ensure that you're only replacing in the portion after the last slash with something like this:

var URL = "https://is5-ssl.mzstatic.com/image/thumb/Music/v4/5c/5a/a0/5c5aa0fd-978e-b038-3918-f6127f97295e/source/30x30bb.jpg";
var urlFixer = (url) => {
  splitArray = url.split("/");
  splitArray[splitArray.length-1] = splitArray[splitArray.length-1].replace("30x30","100x100");
  return splitArray.join("/");
}

console.log(urlFixer(URL));
Sign up to request clarification or add additional context in comments.

Comments

1

let URL = "https://is5-ssl.mzstatic.com/image/thumb/Music/v4/5c/5a/a0/5c5aa0fd-978e-b038-3918-f6127f97295e/source/30x30bb.jpg";

let last_index=URL.lastIndexOf('/');
let substring=URL.substring(last_index + 1);
let replaced=substring.replace(/30/g,"100");
let newURL=URL.substr(0, last_index+1) + replaced;

console.log(newURL);

Comments

1

You can this regex

\/30x30([a-z]*\.\w+)$

enter image description here


let URL = "https://is5-ssl.mzstatic.com/image/thumb/Music/v4/5c/5a/a0/5c5aa0fd-978e-b038-3918-f6127f97295e/source/30x30bb.jpg";

let replace30 = (url) =>{
  return url.replace(/\/30x30([a-z]*\.\w+)$/g, "/100x100"+ "$1")
}

console.log(replace30(URL))
console.log(replace30('http:hello.example.com/400x500bb.jpg'))
console.log(replace30('http:hello.example.com/source/helloworld.jpg'))

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.