If you wish to keep the symbols in the URI, but encode them:

encodedURI = encodeURIComponent(crappyURI);

If you wish to build 'friendly' URIs such as those on blogs:

niceURI = crappyURI.replace(/[^a-zA-Z0-9-_]/g, '');
Answer from Delan Azabani on Stack Overflow
🌐
Programmingbasic
programmingbasic.com › remove-special-characters-of-url
Remove special characters of the URL in JavaScript
March 27, 2025 - And the g (global) flag is used to replace all the occurrences of them. The encodeURI() is a built-in function that encodes special characters in a URL in Javascript. We can use this method to encode any special characters, removing them from ...
Discussions

How to remove special characters from URL in jQuery? - Stack Overflow
I am trying to get a value from URL. The problem is, it may have some special characters in it and I want to remove that. Meaning, http://localhost/DoSomething/Index/123# is my URL. I can get 123#,... More on stackoverflow.com
🌐 stackoverflow.com
javascript - Regexp to remove all special characters in URL - Stack Overflow
i need to basicaly clean urls that have special characters in it, like so: http://172.23.113.79/recherche/pages/Results.aspx?k=**créer*** I would like to replace **créer*** ... More on stackoverflow.com
🌐 stackoverflow.com
September 22, 2011
asp.net - Avoiding special characters in url in javascript - Stack Overflow
i have a treeview in that adding nodes dynamically and adding javscript click function on each node.. here thisFileNode.Value have url values,avoiding spl charcters in url i have used escape func... More on stackoverflow.com
🌐 stackoverflow.com
how can i delete special characters in a url with javascript? - Stack Overflow
1 delete some special character from a string by javascript ... 1 How to remove special characters from a string (URL) using regular expression on wordpress, php, javascript More on stackoverflow.com
🌐 stackoverflow.com
🌐
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;
}

🌐
CoreUI
coreui.io › answers › how-to-remove-special-characters-from-a-string-in-javascript
How to remove special characters from a string in JavaScript · CoreUI
May 20, 2026 - Use replace() with regular expressions to efficiently remove special characters from strings in JavaScript.
🌐
TutorialsPoint
tutorialspoint.com › delete-all-text-up-to-a-character-in-a-url-javascript
Delete all text up to a character in a URL- JavaScript?
January 18, 2023 - In the following example, we use replace() followed by a regular expression to remove all text in a URL up to a character. <!DOCTYPE html> <html> <body> <h1>Delete All Text Up To A Character In A Url JavaScript</h1> <script> var str = "https://www.tutorialspoint.com/index.htm"; document.wr...
Top answer
1 of 3
4
var u=decodeURI("http://172.23.113.79/recherche/pages/Results.aspx?k=cr%c3%83%c2%a9er*");
// u is "http://172.23.113.79/recherche/pages/Results.aspx?k=créer*"

var u=decodeURI("http://172.23.113.79/recherche/pages/Results.aspx?k=cr%C3%A9er*");
// u is "http://172.23.113.79/recherche/pages/Results.aspx?k=créer*"

var u=decodeURI("http://172.23.113.79/recherche/pages/Results.aspx?k=%C3%A9%C3%A8%C3%A0%C3%A7%C3%B9%C3%A2%C3%AA%C3%AE*");
// u is "http://172.23.113.79/recherche/pages/Results.aspx?k=éèàçùâêî*"

Read more:

MDN decodeURI: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/decodeURI

MDN decodeURIComponent: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/decodeURIComponent

 var u=decodeURI("http://172.23.113.79/recherche/pages/Results.aspx?k=%C3%80%C3%81%C3%82%C3%A0%C3%A1%C3%A2%C3%88%C3%89%C3%8A%C3%A8%C3%A9%C3%AA%C3%8C%C3%8D%C3%8E%C3%AC%C3%AD%C3%AE%C3%92%C3%93%C3%94%C3%B2%C3%B3%C3%B4%C3%99%C3%9A%C3%9B%C3%B9%C3%BA%C3%BB*");
 // u is "http://172.23.113.79/recherche/pages/Results.aspx?k=ÀÁÂàáâÈÉÊèéêÌÍÎìíîÒÓÔòóôÙÚÛùúû*"
2 of 3
0

If it's not something very generic and you have special characters and phrases that you want to replace, you could map your special characters/phrases to their replacements and then decode the string and replace each of them:

var replacements = {
    "créer" : "créer", // this is a phrase
    "Ã" : "é",
    "Â" : "e",
    "©" : ""
};
var url = "http://172.23.113.79/recherche/pages/Results.aspx?k=**cr%c3%83%c2%a9er***";
var decoded = unescape(url); // or decodeURI(url);
for(var key in replacements)
    decoded = decoded.replace(key,replacements[key]);
🌐
Stack Overflow
stackoverflow.com › questions › 18464297 › avoiding-special-characters-in-url-in-javascript
asp.net - Avoiding special characters in url in javascript - Stack Overflow
More explanations here. Use decodeURI(str) and decodeURIComponent(str) to get the original string. This thing is that those methods replace special characters that are forbidden in url into escape strings such as & becoming &amp;.
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › encodeURI
encodeURI() - JavaScript - MDN Web Docs
const name = "Ben & Jerry's"; // This is bad: const link = encodeURI(`https://example.com/?choice=${name}`); // "https://example.com/?choice=Ben & Jerry's" console.log([...new URL(link).searchParams]); // [['choice', 'Ben '], [" Jerry's", ''] // Instead: const link = encodeURI( `https://example.com/?choice=${encodeURIComponent(name)}`, ); // "https://example.com/?choice=Ben%20%26%20Jerry's" console.log([...new URL(link).searchParams]); // [['choice', "Ben & Jerry's"]] encodeURI() differs from encodeURIComponent() as follows: ... const set1 = ";/?:@&=+$,#"; // Reserved Characters const set2 = "
🌐
CodePen
codepen.io › jenna_3108 › pen › RXRoQe
Replace all special characters jQuery
You can also link to another Pen ... the CSS 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.
🌐
A Site About Nothing
asiteaboutnothing.net › c_decode-url.php
URL Decoder—URL Converter—Remove special characters from web addresses
This technique is called percent ... of special characters in an html reference and converting the URL manually, you can paste the URLs in the box below and press the "Decode" button....
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