替换传入字符串的重复值

Replace duplicate values of incoming string

我在用重复值替换某些字符串时遇到问题。这是片段:

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

高于 returns 当 url 喜欢 /project/1/cost-estimate 它给了我 .project...cost-estimate。所以我需要将这个 ... 替换为 .project.project -> project.。我该怎么做?有什么建议吗?

您可以在正则表达式中添加一个 + 以使括号内的字符匹配一个或多个字符,有效地 "squeezing" 结果为一个 . 字符:

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