Asked 5 months ago
2
221
In our NextJS application we have a URL that is fed with various query strings.
With some query strings, however, we have the problem that they are displayed in encoded form. For example like this:
As you can see, the colons are replaced with %CA.
I know that this may be a default behavior, but I need the colons in the URL.
Is there any way I can get this? So I need to URL above like:
We are using URLSearchParams() like this:
1
1
0
1
0
When dealing with URLs that contain encoded query strings, it's common to encounter issues with decoding the URL to retrieve the original values. In your case, you're looking to decode the URL to replace %3A with :.
You're already using URLSearchParams() to construct your query string. However, you can use the decodeURIComponent() method to decode the query string before parsing it.
Here's an updated implementation:
In this updated implementation, we use decodeURIComponent() to decode each project string before appending it to the URLSearchParams object.
Another approach is to use the URL.parse() method to parse the URL and then modify the query string to replace %3A with :.
Here's an example:
In this example, we use URL.parse() to parse the original URL and then modify the query string using a regular expression to replace %3A with :.
To decode a URL in JavaScript or NextJS, you can use URLSearchParams() with decodeURIComponent() or URL.parse() with a regular expression to replace encoded characters. Choose the approach that best fits your use case.
Use the decodeURIComponent global function in Javascript
The Result is what you want as below: