Looks like what your trying to do is url encode your special characters just use the functions:

  • encodeURIComponent
  • encodeURI

depending on whether you are encoding an entire url or just a component. e.g.

encodeURIComponent("as686sa8d6sa8787^%^%$^£$%£$%");
//Output: "as686sa8d6sa8787%5E%25%5E%25%24%5E%C2%A3%24%25%C2%A3%24%25"
Answer from ShaneQful on Stack Overflow
🌐
Reddit
reddit.com › r/nextjs › is there a simple way to remove special characters from urls
r/nextjs on Reddit: Is there a simple way to remove special characters from urls
August 3, 2023 -

In my nextjs 13 app I am using the generateStaticParams() method. The issue is a lot of the "urls" have special characters. Such as spaces or even question marks lol.

I'm wondering if there's a simple solution or do I need to write a regex?

When I was using nextjs 12 with getStaticPaths I never had any issues for some reason.

An example of the code is

const Page = ({params}) => {
    const {slug} = params;
}

Discussions

Replace all special characters like 'é' in URL with JavaScript - Stack Overflow
I'm receiving response from an API that contains name strings with special letters like 'é'. Then I need to make a request to another API with query string containing this name with 'é'. API is th... More on stackoverflow.com
🌐 stackoverflow.com
jquery - Remove illegal URL characters with JavaScript - Stack Overflow
days". I use the values in the array to create some url's and need to remove the /\ and other illegal URL More on stackoverflow.com
🌐 stackoverflow.com
regex - javascript : how to replace special symbols/characters that will break your query string in your URL - Stack Overflow
It works in normal circumstances. It didn't work when I pass that url to a server page for security check such as 'SafeWay.aspx?' + myURL. Probably my SafeWay app didn't decode those encode correctly. To quick fix for my production (for now), I just replaced those sensitive characters in one-line. More on stackoverflow.com
🌐 stackoverflow.com
Why do we need to replace `:`, `$`, `,`, `+`, `[`, and `]` back
There was an error while loading. Please reload this page · https://github.com/axios/axios/blob/v1.x/lib/helpers/buildURL.js#L14 More on github.com
🌐 github.com
4
June 2, 2023
🌐
Programmingbasic
programmingbasic.com › remove-special-characters-of-url
Remove special characters of the URL in JavaScript
March 27, 2025 - Next using encodeURIComponent() we have encoded all the characters in the URL by effectively removing any special characters from it and replacing it with their hexadecimal value. In this article, we learned how to remove any special character from a given URL using JavaScript.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › encodeURIComponent
encodeURIComponent() - JavaScript - MDN Web Docs
.replace( /['()*]/g, (c) => `%${c.charCodeAt(0).toString(16).toUpperCase()}`, ) // The following are not required for percent-encoding per RFC5987, // so we can allow for a little better readability over the wire: |`^ .replace(/%(7C|60|5E)/g, (str, hex) => String.fromCharCode(parseInt(hex, 16)), ) ); } The more recent RFC3986 reserves !, ', (, ), and *, even though these characters have no formalized URI delimiting uses. The following function encodes a string for RFC3986-compliant URL component format. It also encodes [ and ], which are part of the IPv6 URI syntax. An RFC3986-compliant encodeURI implementation should not escape them, which is demonstrated in the encodeURI() example.
Find elsewhere
🌐
GitHub
github.com › axios › axios › issues › 5728
Why do we need to replace `:`, `$`, `,`, `+`, `[`, and `]` back · Issue #5728 · axios/axios
June 2, 2023 - /** * It replaces all instances of the characters `:`, `$`, `,`, `+`, `[`, and `]` with their * URI encoded counterparts * * @param {string} val The value to be encoded. * * @returns {string} The encoded value.
Author   axios
🌐
CodePen
codepen.io › avnishjayaswal › pen › oNYeREe
replace special characters using JavaScript
You can also link to another Pen ... from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Top answer
1 of 2
22

You have 3 options:

escape() will not encode: @*/+

encodeURI() will not encode: ~!@#$&*()=:/,;?+'

encodeURIComponent() will not encode: ~!*()'

But in your case, if you want to pass a url into a GET parameter of other page, you should use escape or encodeURIComponent, but not encodeURI.

2 of 2
9

To be safe and ensure that you've escaped all the reserved characters specified in both RFC 1738 and RFC 3986 you should use a combination of encodeURIComponent, escape and a replace for the asterisk('*') like this:

encoded = encodeURIComponent( parm ).replace(/[!'()]/g, escape).replace(/\*/g, "%2A");

[Explanation] While RFC 1738: Uniform Resource Locators (URL) specifies that the *, !, ', ( and ) characters may be left unencoded in the URL,

Thus, only alphanumerics, the special characters "$-_.+!*'(),", and reserved characters used for their reserved purposes may be used unencoded within a URL.

RFC 3986, pages 12-13, states that these special characters are reserved as sub-delimiters.

reserved = gen-delims / sub-delims

gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@"

sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="

The escape() function has been deprecated but can be used to URL encode the exclamation mark, single quote, left parenthesis and right parenthesis. And since there is some ambiguity on whether an asterisk must be encoded in a URL, and it doesn't hurt to encode, it you can explicitly encode is using something like the replace() function call. [Note that the escape() function is being passed as the second parameter to the first replace() function call. As used here, replace calls the escape() function once for each matched special character of !, ', ( or ), and escape merely returns the 'escape sequence' for that character back to replace, which reassembles any escaped characters with the other fragments.]

Also see 'https://stackoverflow.com/questions/6533561/urlencode-the-asterisk-star-character'

Also while some websites have even identified the asterkisk(*) as being a reserved character under RFC3986, they don't include it in their URL component encoding tool.

Unencoded URL parms:

parm1=this is a test of encoding !@#$%^&*()'
parm2=note that * is not encoded

Encoded URL parms:

parm1=this+is+a+test+of+encoding+%21%40%23%24%25%5E%26*%28%29%27
parm2=note+that+*+is+not+encodeds+not+encoded
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › encodeURI
encodeURI() - JavaScript - MDN Web Docs
The encodeURI() function encodes a URI by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two surrogate characters).
🌐
W3Schools
w3schools.com › tags › ref_urlencode.ASP
HTML URL Encoding Reference
URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits. URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with .
🌐
Metring
ricardometring.com › javascript-replace-special-characters
JavaScript: Replacing Special Characters - The Clean Way
The only addition, in this case, was to create 2 groups in the regex through ([ group 1 ]|[ group 2 ]) and add to group 2 the regular expression [^0-9a-zA-Z], which means: anything that's not (^) 0-9, a-z or A-Z, is also replaced. ... Another quite recurrent use case is the need to clear the accents and then replace special characters with some other one, e.g.
🌐
thisPointer
thispointer.com › home › javascript › javascript: replace special characters in a string
Javascript: Replace special characters in a string - thisPointer
June 22, 2021 - Javascript is a language, This is the most popular language. ... Replace all occurrences of all special characters with “_” (underscore) from the string “Javascript is @ a # language, This is : the most %popular _ language.”