Home > Net >  How can I delete one part of the string without deleting the same set of characters in another place
How can I delete one part of the string without deleting the same set of characters in another place

Time:04-26

I'm trying to clean up a string (URL). I need that every time it contains ipfs://ipfs/ to replace it by a single ipfs/. The full string is ipfs://ipfs/QmR6xjBCn but it should be ipfs://QmR6xjBCn In some cases I get string that are already clean, in other cases I dont'.

CodePudding user response:

Use the replace() function. If the text is not in the string then it won't replace anything. So it only works when the double ipfs is there.

let str;
// example one
str = "ipfs://ipfs/QmR6xjBCn";
str = str.replace("ipfs://ipfs/","ipfs://");
console.log(str);

// example two, same code different url
str = "ipfs://SomethingRandom";
str = str.replace("ipfs://ipfs/","ipfs://");
console.log(str);

CodePudding user response:

if (fixedURL.includes('ipfs://ipfs')) {
  fixedURL2 = fixedURL.replace('ipfs://ipfs','ipfs://')

  obj.imageUrl = fixedURL2;
} else {

  obj.imageUrl = fixedURL;
}
  • Related