Javascript encodeURIComponent doesn't encode single quotes

后端 未结 6 1435
刺人心
刺人心 2021-02-01 03:39

Try it out:

encodeURIComponent(\"\'@#$%^&\");

If you try this out you will see all the special characters are encoded except for the single

6条回答
  •  执念已碎
    2021-02-01 03:59

    I found a neat trick that never misses any characters. I tell it to replace everything except for nothing. I do it like this (URL encoding):

    function encode(w){return w.replace(/[^]/g,function(w){return '%'+w.charCodeAt(0).toString(16)})}
    

    function encode(w){return w.replace(/[^]/g,function(w){return '%'+w.charCodeAt(0).toString(16)})}
    
    loader.value = encode(document.body.innerHTML);

提交回复
热议问题