JS filter url parameters of the implementation of special characters

  • 2020-03-30 01:01:27
  • OfStack

In the URL pass parameter, there are some special characters, and these symbols cannot be directly passed in the URL, if you want to pass these special symbols in the URL, then use their encoding.
Some URL special symbols and coded hexadecimal values are listed in the following table
1. The + sign in the + URL indicates the space %2B
2. The Spaces in the URL can be + or encoded with %20
3. / separate directories and subdirectories %2F
4.? Separate the actual URL from the parameter %3F
5. % specifies the special character %25
6. # indicates bookmark %23
7. & the separator %26 between the parameters specified in the URL
8. = the value %3D of the parameter specified in the URL

Solution:
Replace () method if you use STR. Replace ("-","!") ) replaces only the first matched character.
And STR. Replace (/ \ - / g, "!" ) can replace all matched characters (g is the global flag).

The replace ()

The replacement character variable in js is as follows:
Data2 = data2. Replace (/ \ % / g, "% 25");
Data2 = data2. Replace (/ \ # / g, "% 23");
Data2 = data2. Replace (/ \ & / g, "% 26");


Related articles: