-4
說我有一個字符串:var str="localhost:20012/prj/duck/duck/woohoo"
。替換所有在JavaScript中相同的單詞?
正如您可能已經注意到在上述字符串處有2 /duck
。供參考:可能還有更多。
我基本上想要的是刪除所有與見好就收要那麼它會是這樣的:localhost:20012/prj/duck/woohoo
說我有一個字符串:var str="localhost:20012/prj/duck/duck/woohoo"
。替換所有在JavaScript中相同的單詞?
正如您可能已經注意到在上述字符串處有2 /duck
。供參考:可能還有更多。
我基本上想要的是刪除所有與見好就收要那麼它會是這樣的:localhost:20012/prj/duck/woohoo
您可以使用split
方法,然後就通過傳遞的回調提供的函數刪除使用filter
方法重複。
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);
https://stackoverflow.com/questions/16843991/remove-occurrences-of-duplicate-words-in-a-string –
@Whatever,它是爲你工作? –