我正在嘗試清理字串(URL)。我需要每次它包含ipfs://ipfs/用單個ipfs/. 完整的字串是ipfs://ipfs/QmR6xjBCn
,但它應該是 ipfs://QmR6xjBCn
在某些情況下,我得到的字串已經干凈,在其他情況下我沒有。
uj5u.com熱心網友回復:
使用替換()函式。如果文本不在字串中,則它不會替換任何內容。所以它只有在雙 ipfs 存在時才有效。
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);
uj5u.com熱心網友回復:
if (fixedURL.includes('ipfs://ipfs')) {
fixedURL2 = fixedURL.replace('ipfs://ipfs','ipfs://')
obj.imageUrl = fixedURL2;
} else {
obj.imageUrl = fixedURL;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/463979.html
標籤:javascript 正则表达式 细绳
上一篇:SequelizefindAlluserswithIdbutreturnusersfromdifferentcolumn
下一篇:R子集資料幀遵循基于日期的規則
