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 :
.
Using URLSearchParams()
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.
Alternative Approach: Using URL.parse()
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 :
.
Conclusion
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.