Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript encodeURIComponent doesn't encode single quotes

Tags:

javascript

Try it out:

encodeURIComponent("'@#$%^&");

If you try this out you will see all the special characters are encoded except for the single quote. What function can I use to encode ALL the characters and use PHP to decode them?

Thanks.

like image 727
Ilya Karnaukhov Avatar asked Jun 05 '12 11:06

Ilya Karnaukhov


People also ask

How do you handle a single quote in a URL?

encodeURIComponent does not encode the single quote (apostrophe) because, according to RFC 3986, the apostrophe does not need to be encoded in any part of the URL. (It is a 'reserved' character, but carries no special meaning in this context.)

Should I use encodeURI or encodeURIComponent?

encodeURIComponent should be used to encode a URI Component - a string that is supposed to be part of a URL. encodeURI should be used to encode a URI or an existing URL.

How do you escape a single quote in JavaScript?

Using the Escape Character ( \ ) We can use the backslash ( \ ) escape character to prevent JavaScript from interpreting a quote as the end of the string. The syntax of \' will always be a single quote, and the syntax of \" will always be a double quote, without any fear of breaking the string.

What is the difference between encodeURI and encodeURIComponent?

The difference between encodeURI and encodeURIComponent is encodeURIComponent encodes the entire string, where encodeURI ignores protocol prefix ('http://') and domain name. encodeURIComponent is designed to encode everything, where encodeURI ignores a URL's domain related roots.


4 Answers

I'm not sure why you would want them to be encoded. If you only want to escape single quotes, you could use .replace(/'/g, "%27"). However, good references are:

  • When are you supposed to use escape instead of encodeURI / encodeURIComponent?
  • Comparing escape(), encodeURI(), and encodeURIComponent() at xkr.us
  • Javascript Madness: Query String Parsing #Javascript Encode/Decode Functions
like image 144
Bergi Avatar answered Sep 21 '22 19:09

Bergi


You can use:

function fixedEncodeURIComponent (str) {   return encodeURIComponent(str).replace(/[!'()*]/g, escape); }  fixedEncodeURIComponent("'@#$%^&"); 

Check reference: http://mdn.beonex.com/en/JavaScript/Reference/Global_Objects/encodeURIComponent.html

like image 28
Biswajit Maji Avatar answered Sep 21 '22 19:09

Biswajit Maji


You can use btoa() and atob(), this encodes and decodes the given string including single quote.

like image 36
Karthik Ashwin Avatar answered Sep 24 '22 19:09

Karthik Ashwin


Just try encodeURI() and encodeURIComponent() yourself...

console.log(encodeURIComponent('@#$%^&*'));

Input: @#$%^&*. Output: %40%23%24%25%5E%26*. So, wait, what happened to *? Why wasn't this converted? TLDR: You actually want fixedEncodeURIComponent() and fixedEncodeURI(). Long-story...

encodeURIComponent() : Do not use. Use fixedEncodeURIComponent(), as defined and explained by the MDN encodeURIComponent() Documentation, emphasis mine...

To be more stringent in adhering to RFC 3986 (which reserves !, ', (, ), and *), even though these characters have no formalized URI delimiting uses, the following can be safely used:

function fixedEncodeURIComponent(str) { return encodeURIComponent(str).replace(/[!'()*]/g, function(c) { return '%' + c.charCodeAt(0).toString(16); }); }

While we're on the topic, also don't use encodeURI(). MDN also has their own rewrite of it, as defined by the MDN encodeURI() Documentation. To quote their explanation...

If one wishes to follow the more recent RFC3986 for URLs, which makes square brackets reserved (for IPv6) and thus not encoded when forming something which could be part of a URL (such as a host), the following code snippet may help:

function fixedEncodeURI(str) { return encodeURI(str).replace(/%5B/g, '[').replace(/%5D/g, ']'); }

like image 29
HoldOffHunger Avatar answered Sep 20 '22 19:09

HoldOffHunger