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.
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.)
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.
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.
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.
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:
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
You can use btoa()
and atob()
, this encodes and decodes the given string including single quote.
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, ']'); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With