Escape (), encodeuri (), and encodeURIComponent () are the three common methods used in JavaScript to encode strings, but their similarities and differences have plagued many JavaScript beginners, I'm here today to analyze and compare these three methods in detail. escape () method msdn jscript reference said: the escape method returns a string value (In unicode format) that contains the contents of [the argument]. All spaces, punctuation, accented characters, and any other non-ascii characters are replaced with %xx encoding, where xx is equivalent to the hexadecimal number representing the character. for example, a space is returned as "%20." The Escape method returns a string that contains the contents of the passed-in parameter in Unicode format. The escape method replaces all whitespace, punctuation, accented characters, and any other non-ASCII characters in the passed-in parameter with the encoded form of%XX, where xx is the same as the 16-digit representation of the character it represents. If the 16 binary representation of the space character is 0x20, then xx should be 20, that is, Escape (" ?) Return to "%20". mozilLa developer core javascript guide said: the escape and unescape Functions let you encode and decode strings. the escape function returns the hexadecimal encoding of an argument in the iso latin character set. the unescape function returns the ascii string for the specified hexadecimal encoding value. Escape and Unescape methods can help you encode and decode strings. The Escape method returns its 16 encoding for a parameter that consists of characters in the Iso latin character set. Correspondingly, the Unescape method can convert the parameters of the 16-encoding form into its ASCII form. encodeuri () method msdn jscript reference said: the encodeuri method Returns an encoded uri. if you pass the result to decodeuri, the original string is returned. The encodeURI method does Not encode the following characters: ":", "/", ";", and "?". Use encodeURIComponent to encode these characters. The encodeURI method returns an encoded URI. If the encoding result of the encodeURI method is passed to the decodeURI method as a parameter, the original non-encoded string can be obtained. It is important to note that the encodeURI method does not encode the following characters ":", "/", ";", and "?". If you want to encode these characters, use the encodeURIComponent method. mozilla developer core javascript guide said: encodes a uniform resource identifier (URI) by replacing each instance of certain characters by one, two, or three escape sequences representing the utf-8 encoding of the character.
Escape (), encodeURI () and encodeURIComponent () in JavaScript