Javascript encodeURIComponent doesn't encode single quotes

后端 未结 6 1434
刺人心
刺人心 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 04:23

    As @Bergi wrote, you can just replace all the characters:

    function encoePicture(pictureUrl)
    {
     var map=
     {
              '&': '%26',
              '<': '%3c',
              '>': '%3e',
              '"': '%22',
              "'": '%27'
     };
    
     var encodedPic = encodeURI(pictureUrl);
     var result = encodedPic.replace(/[&<>"']/g, function(m) { return map[m];});
     return result;
    }
    

提交回复
热议问题