分割网址并替换

时间:2019-03-27 21:50:11

标签: javascript reactjs

这是我的网址:

/ shop / rest / model / atg / commerce / catalog / ProductCatalogActor / getHardwareFamilySkus?skuId = sku1431389

我需要将网址分为以下两个不同的部分

abc => / shop / rest / model / atg / commerce / catalog / ProductCatalogActor /

pqr => getHardwareFamilySkus?skuId = sku1431389

1 个答案:

答案 0 :(得分:0)

一种可能的解决方案是将String.match()与某些捕获组配合使用,例如:

const url = "/shop/rest/model/atg/commerce/catalog/ProductCatalogActor/getHardwareFamilySkus?skuId=sku1431389";

const customSplit = (url) =>
{
    let matches = url.match(/^(\/.*\/)(.*)$/);
    return [matches[1], matches[2]];
}

let [abc, pqr] = customSplit(url);

console.log("abc => " + abc);
console.log("pqr => " + pqr);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}