-4

Say I have a string: var str="localhost:20012/prj/duck/duck/woohoo".

As you might have noticed there is 2 /duck at the above string. FYI: There could be more.

What I basically Want is to remove all and just leave want so its gonna look like: localhost:20012/prj/duck/woohoo

Whatever
  • 125
  • 2
  • 2
  • 8

1 Answers1

0

You can use split method and then just remove the duplicates using filter method by passing a callback provided function.

var str="localhost:20012/prj/duck/duck/woohoo";
str=str.split('/').filter(function(item,index,str){
  return str.indexOf(item)==index;
}).join('/');
console.log(str);
JJJ
  • 32,902
  • 20
  • 89
  • 102
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128