0

I have URLs like below with customer ID etc. I want to remove them and put an asterisk instead of them. Below examples will give you a better idea.

/paas/service/3.0/part/d878bc1e-9bb8-4a0e/log/account/7367c100-9390-11e3/resource/AdminHistory --> /paas/service/3.0/part/*/log/account/*/resource/AdminHistory

/hs/service/3.0/bootstrap/mnd/3388959354 --> /hs/service/3.0/bootstrap/mnd/*

/paas/service/3.0/part/d878bc1e-9bb8-4a0e-b224-a4ba0d7dfcec/asset --> /paas/service/3.0/part/*/asset

/paas/service/3.0/part/3c5631df-52c9/servicepolicy/8ee6ba08-8d7a/carrierPlan/c2b4a364-98e3-4546 --> /paas/service/3.0/part/*/servicepolicy/*/carrierPlan/*

The IDs are either numeric or alphanumeric. How should i approach this ?

1
  • You can just do something like: url.replace(new RegExp(id, 'g'), '*'). Commented Oct 18, 2016 at 0:29

1 Answer 1

1

The IDs are either numeric or alphanumeric.

All of the parts in your example URLs contain either letters or digits, so you need to know what the text is before the ID so that you can target that plus the ID that follows.

So perhaps something like this:

function modifyURL(url) {
  return url.replace(/(\/(?:part|account|mnd|servicepolicy|carrierPlan)\/)[^\/]+/g, "$1*");
}

console.log(modifyURL("/paas/service/3.0/part/d878bc1e-9bb8-4a0e/log/account/7367c100-9390-11e3/resource/AdminHistory"));
console.log(modifyURL("/hs/service/3.0/bootstrap/mnd/3388959354"));
console.log(modifyURL("/paas/service/3.0/part/d878bc1e-9bb8-4a0e-b224-a4ba0d7dfcec/asset"));
console.log(modifyURL("/paas/service/3.0/part/3c5631df-52c9/servicepolicy/8ee6ba08-8d7a/carrierPlan/c2b4a364-98e3-4546"));

To explain the regex I've used:

(       // start of capturing submatch         
\/      // match a forward slash
(?:     // start of non-capturing group       
part|account|mnd|servicepolicy|carrierPlan   // match one of those words
)       // end of non-capturing group
\/      // match forward slash
)       // end of capturing submatch
[^\/]+  // match one or more of anything but a forward slash
Sign up to request clarification or add additional context in comments.

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.