Best way to split the query params that is present in a string and then encode

I have few strings that could be in this format:

const s1 = "https://test1.com/testPage";
const s2 = "https://test2.com/testPage?specificParam=1"
const s3 = "https://test3.com/someOtherPage?specificParam=2&restParam=3";

So when i read it in a function, I have to check if a string has query params included, if yes then I have to encode it using it window.btoa() to all the query params. If not directly redirect. Can someone assist as to how to do this?

function test(path){
   const href = window.location.origin + path;
   if(href.includes('?')){
      // split all the query params and then attach `btoa` to it and then openthe url 
       with encoded params
   }
   window.open(href, '_blank', 'no-referer');
}