Home > Web Front-end > Front-end Q&A > What are the javascript escape functions?

What are the javascript escape functions?

青灯夜游
Release: 2023-01-06 11:18:01
Original
2894 people have browsed it

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);
Copy after login

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);
Copy after login

For example:

   escape("Hello{[World]}");
Copy after login

The escape result is:

Hello%7B%5BWorld%5D%7D
Copy after login
 unescape("Hello%7B%5BWorld%5D%7D")
Copy after login

The return result is:

Hello{[World]}
Copy after login

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)
Copy after login

URIString is an encoded URI component.

encodeURIComponent(URIString)
Copy after login

Example:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template