Why does decodeURIComponent('%') lock up my browser?

后端 未结 7 1148
别那么骄傲
别那么骄傲 2020-12-31 05:11

I was just testing something with AJAX and I found that on success if I alert

alert(decodeURI(\'%\'));

or

alert(encodeURIC         


        
相关标签:
7条回答
  • 2020-12-31 05:47

    I had the same issue as OP and found this useful topic. Had to find a way to check if URI string contained percent sign before using decodeURIComponent().

    The piece of code from Ilia Rostovtsev works great except for URI which contains encoded characters like %C3 (where percent sign is starting by [A-F]) because the regex used doesn't handle them (only % followed by a decimal).

    I replaced the following line:

       x = mod ? arr[i].replace(/%(?!\d+)/g, '%25') : arr[i];
    

    by

       x = mod ? arr[i].replace(/%(?!\d|[ABCDEF]+)/g, '%25') : arr[i];
    

    Now, it is working as the regex will reject %1A to %9F and also %A1 to %F9

    0 讨论(0)
提交回复
热议问题