Search code examples
javascripturlencodingurlencodeurldecode

Check if part of URL is encoded


i've the following URL and I decode it which is working fine but the issue is that part of the URL is not encoded, there is a way to check which part of the URL is encoded and Decode just it. I need to decode it and do some logic and then encode it again and the problem is that I encode parts which doesn't need to encoded

for example this is the URL

http%3A%2F%2Fm-d6fe73.m.corp%3A506%2Flin%2Fcalack&client_id=ts2.node

As you can see the calack&client_id=ts2.node is not encoded here and it can be any parameter in the URL


Solution

  • This should work:

    var url = 'http%3A%2F%2Fm-d6fe73.m.corp%3A506%2Flin%2Fcalack&client_id=ts2.node';
    var result = url.split('&').map(function(item){
       return (item.indexOf('%')>=0)? decodeURIComponent(item) : item;
    });
    console.log(result.join('&')); // will give you: http://m-d6fe73.m.corp:506/lin/calack&client_id=ts2.node