Home  >  Article  >  Web Front-end  >  What are the javascript escape functions?

What are the javascript escape functions?

青灯夜游
青灯夜游Original
2021-06-23 15:06:492828browse

Escape function: 1. escape() function, which can replace all spaces, punctuation, accents and other non-ASCII characters with "%xx" hexadecimal sequence code; 2. encodeURI() function , the string can be encoded as a URI; 3. encodeURIComponent() function.

What are the javascript escape functions?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

There are many functions in js that can be used to escape and de-escape, such as escape() and unescape(), encodeURI() and decodeURI(), encodeURIComponent() and decodeURIComponent(), etc.

1.escape() and unescape()

The escape() function converts the parameter into a string and in URL-encoded format Encode. In this format, all spaces, punctuation, accents, and other non-ASCII characters are replaced by the %xx hexadecimal sequence code. If a space is returned.

   escape(param);

The parameter param is an expression converted to a string and encoded in URL-encoded format.

unescape() function is just the opposite, decoding the URL encoding format string and returning a string.

   unescape(param);

For example:

   escape("Hello{[World]}");

The escape result is:

Hello%7B%5BWorld%5D%7D
 unescape("Hello%7B%5BWorld%5D%7D")

The return result is:

Hello{[World]}

Disadvantages: escape() and unescape() Only ASCII characters can be processed. It is not recommended to use these two methods for escaping and decoding.

2.encodeURI() and decodeURI()

encodeURI() function encodes a text string into a valid unified Resource identifier (URI). And decodeURI() decodes the URI into a normal string.

encodeURI(param);

param is a string that will be encoded

decodeURI(param);

Just the opposite

For example:

encodeURI("Hellow{[World]}")

The escape result is: Hello{[World]}

decodeURI("Hello{[World ]}")

Return result: Hello{[World]}.

Note:

encodeURI() function alone cannot form an HTTP GET request or POST request, because &, , = will not be transcoded , these characters are treated as special characters in GET or POST requests.

3.encodeURIComponent() and decodeURIComponent()

These two functions are also used to encode and decode

decodeURIComponent(URIString)

URIString is an encoded URI component.

encodeURIComponent(URIString)

Example:

var aa='哈哈+呵呵';
encodeURIt(aa)  //'哈哈 呵呵'
encodeURIComponent(aa)  //'哈哈+呵呵'

encodeURIComponent() can convert all characters except English letters, numbers, -,_,.,!,~,*,',(,)

For more programming-related knowledge, please visit: Programming Video! !

The above is the detailed content of What are the javascript escape functions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn