Exclamation-point encodeUriComponent?

Native encodeURIComponent does not support exclamation point coding ! which I need to have in the url request url correctly.

node.js querystring.stringify() does not work either.

is the only way to use a custom function, for example - https://github.com/kvz/phpjs/blob/master/functions/url/urlencode.js#L30 ?

+4
source share
1 answer

You can override your own function to add this functionality.

Here is an example of the encodeURIComponent extension to handle exclamation points.

 // adds '!' to encodeURIComponent ~function () { var orig = window.encodeURIComponent; window.encodeURIComponent = function (str) { // calls the original function, and adds your // functionality to it return orig.call(window, str).replace(/!/g, '%21'); }; }(); encodeURIComponent('!'); // %21 

You can also add a new function if you want the code to be shorter.
This is for you, however.

 // separate function to add '!' to encodeURIComponent // shorter then re-defining, but you have to call a different function function encodeURIfix(str) { return encodeURIComponent(str).replace(/!/g, '%21'); } encodeURIfix('!'); // %21 

Additional examples of this can be found on the Mozilla Developer Website

+6
source

Source: https://habr.com/ru/post/1502483/


All Articles