Problem description
There is a discussion about the encodeURIComponent () function application before [note JavaScript passes the complete] URL data, which is mainly discussed in GBK encoded pages using encodeURIComponent () The function encoding URI generates garbled characters by submitting the Chinese character via Ajax.
Problem solving
The purpose of using the encodeURIComponent () function is to encode special characters,/? : @ & = + $ #, but the actual application may not need to encode Chinese characters, so you can write a custom function instead of the encodeuricomponent () function.
Using the encodeURIComponent () function code,/? : @ & = + $ # character%2c%2f%3f%3a%40%26%3d%2b%24% 23, so just use the JavaScript regular expression replace () function to replace the preceding few special characters with the corresponding encoding.
encodeURIComponent () function substitution scheme for encoding special characters only
code is as follows |
copy code |
function encodeuristr (str) { str = str.replace (/,/g, '%2c ') ; str = str.replace (///g, '%2f '); str = str.replace (/?/g, '%3f '); str = str.replace (/:/g, '%3a '); str = str.replace (/@/g, '%40 '); str = str.replace (/&/g, '%26 '); str = str.replace (/=/g, '%3d '); str = str.replace (/+/g, '%2b '); str = str.replace (/$/g, '%24 '); str = str.replace (/#/g, '%23 '); return str; } |
The main function of the above is to analyze the implementation of the mechanism, you can streamline the ENCODEURISTR () function as follows
The code is as follows |
Copy Code |
function Encodeuristr (str) { var regexs = new Array (New RegExp (', ', ' G '), New RegExp ('/', ' G '), New RegExp (' \? ', ' G '), New RegExp (': ', ' G '), New RegExp (' @ ', ' g '), New RegExp (' & ', ' G '), New RegExp (' = ', ' g '), New RegExp (' \+ ', ' G '), New RegExp (' \$ ', ' G '), New RegExp (' # ', ' G ') ); var replaces = new Array ('%2c ', '%2f ', '%3f ', '%3a ', '%40 ', '%26 ', '%3d ', '%2b ', '%24 ', '%23 '); for (var i = 0; i < regexs.length; i++) { str = str.replace (Regexs[i], replaces[i]); } return str; } |
Implements a custom Encodeuristr () function to encode special characters instead of the encodeuricomponent () function. Applies to data exchange with the server.
Note that this substitution mechanism is specific, only for special characters (,/?). : @ & = + $ #) to encode, implementation does not really replace the encodeuricomponent () function.
Refer to replace
The replace () method replaces some characters in a string with some other characters, or replaces a substring that matches a regular expression.
Grammar
Stringobject.replace (regexp/substr,replacement) parameter description
Regexp/substr required. The RegExp object that prescribes the substring or pattern to be replaced.
Note that if the value is a string, it is used as the direct text pattern to retrieve, not first converted to the RegExp object.
Replacement required. A string value. Provides a function that replaces text or generates alternate text.
return value
A new string that is replaced with replacement for the first match of RegExp or after all matches have been made.
Description
The replace () method of the string Stringobject performs a find-and-replace operation. It will look for substrings in the stringobject that match the regexp, and then replace them with replacement. If RegExp has global flag G, then the Replace () method replaces all matching substrings. Otherwise, it replaces only the first matching substring.
Replacement can be either a string or a function. If it is a string, then each match is replaced by a string. However, the $ character in replacement has a specific meaning. As shown in the following table, it shows that the string that is matched from the pattern will be used for substitution.
Character replacement text
$ 、...、 $99 The text that matches the 1th to 99th subexpression in the RegExp.
$& the substring that matches the regexp.
$ ' is the text on the left side of the matching substring.
$ ' is the text on the right side of the matching substring.
$$ Direct measure symbol.
Note: ECMAScript v3 stipulates that the parameter replacement of the replace () method can be a function rather than a string. In this case, each match calls the function, and the string it returns is used as the replacement text. The first parameter of the function is a string that matches the pattern. The next argument is a string that matches the subexpression in the pattern, and can have 0 or more of these parameters. The next argument is an integer that declares where the match appears in the Stringobject. The last parameter is the stringobject itself.