There are a number of ways to do this, but the approach I've used in the past with something like this is to use a Third Party Plug In Action to do the heavy lifting.
The plug in is called Split and you can download it from the Plug In Actions category of this forum; find it here:
Once you've installed it you can easily split out the bits from the URL and reassemble them. Here is a demo macro for you to use as a basis for your own solution.
If you install it, it will appear in the Temporary group and will be enabled.
It acts on the URL of the frontmost browser tab and puts the clean result on the clipboard. The browser itself does not have to be the active application, nor does the original URL have to be on the clipboard before you run the macro. At present, I believe the browsers for which this works are limited to Safari and Chrome. See the wiki for info on the FrontBrowserURL token.