【问题标题】:How to decode a URL encoded like this in JavaScript如何在 JavaScript 中解码这样编码的 URL
【发布时间】:2020-07-23 12:34:59
【问题描述】:

我有一个带有此功能的脚本来编码 URL,但我不知道如何解码它

function app_base64_encode(e) {
  return btoa(
    encodeURIComponent(e).replace(/%([0-9A-F]{2})/g, function(e, n) {
      return String.fromCharCode("0x" + n);
    })
  );
}

function app_base64_decode(e) {
  //...
}

console.log(app_base64_encode("https://stackoverflow.com/questions/63054396/how-to-decode-a-url-encoded-like-this-in-javascript"))

【问题讨论】:

  • 发布示例 URL 及其编码格式会有所帮助
  • 您的替换撤销了 encodeURIComponent 所做的事情。有什么意义?
  • @tkausl 我不是这个函数的创建者,我不知道它是否真的可以,但我希望能够解码它的 URL。对不起我的英语不好,谢谢!

标签: javascript decode


【解决方案1】:

看来atob就够了

function app_base64_encode(e) {
  return btoa(
    encodeURIComponent(e).replace(/%([0-9A-F]{2})/g, function(e, n) { // undo the encode for some reason
      return String.fromCharCode("0x" + n);
    })
  );
}

function app_base64_decode(e) {
  return atob(e)
}

const url = app_base64_encode("%0A%0D") 

//https://stackoverflow.com/questions/63054396/how-to-decode-a-url-encoded-like-this-in-javascript%0A%0D");

console.log(url)
console.log(app_base64_decode(url))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-02
    • 1970-01-01
    • 2015-05-26
    • 2015-03-03
    • 2012-08-29
    • 1970-01-01
    • 2018-05-19
    相关资源
    最近更新 更多