Search code examples
regextypescriptangular-router

Replace duplicate values of incoming string


I have a problem with replacing some strings with duplicated values. Here is snippet:

const firstStep = this.router.url.replace(/[\d\/]/g, '.');

Above returns when url like /project/1/cost-estimate it gives me .project...cost-estimate. So i need to replace this ... to . or project.project -> project.. How i do that ? Any advice ?


Solution

  • You can add a + to your regex to make the characters inside the bracket match one or more, effectively "squeezing" the result to one . character:

    console.log("/project/1/cost-estimate".replace(/[\d\/]+/g, '.'));