Rahul Nagrale Rahul Nagrale -4 years ago 95
Javascript Question

Check if URL has repeating strings and remove them if any?

I am looking for the way to remove the duplicate string from any url in

jQuery
.


example url: http://localhost:33224/abc/abc/xyz.xml


Can anyone here please advise how to remove the duplicate strings from any url using
jQuery
?

Answer Source

var url="http://localhost:33224/abc/abc/xyz.xml";

var arr=url.split('/');
var newArr=[];
var newUrl='';
$.each(arr,function(i,item){
if($.inArray(item,newArr) == -1)
{
newArr.push(item);
}
});

$.each(newArr,function(i,item){
newUrl += item+'/';
});

console.log(newUrl)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download