为了账号安全,请及时绑定邮箱和手机立即绑定

无法正确解码/编码 Url

无法正确解码/编码 Url

慕少森 2022-12-22 11:53:34
我正在研究一个 Url 解码和编码系统。但由于一些奇怪的原因,它只会解码/编码特定的字符串。此外,它似乎在字符串中包含特定片段时对其进行解码/编码。这很难解释,但对于这可能行不通的事情却很困惑。我试着弄清楚它的问题,但这只会让整个问题看起来更不合逻辑。我希望有人可以帮助我。有解释。如果代码与它的风格相同,我会喜欢它。我也知道网上可能有一些软件包可以轻松做到这一点,但我宁愿自己制作。这是我可以更多地练习 JS 的一种方式。// I know I don't have all the characters markek. I am doing that latervar Url = {  filterEncode : ["%2B","%3A","%3D","%3F","%2F","%26","%252F","%253A","%253D","%253F","%252B"],  filterDecode : ["+",":","=","?","/","&","%2F","%3A","%3D","%3F","%2B"],  decode : function(decodeText){    let returnString, a, b;    let filterEncode = Url.filterEncode;    let filterDecode = Url.filterDecode;    for (a = 0; a < filterEncode.length; a++){      let regexEn = new RegExp(filterEncode[a],"g");      let regexDe = new RegExp("/" + filterDecode[a],"g");      let regex = new RegExp(regexEn,"g");      let array = (decodeText.match(regex) || []).length      for (b = 0; b < array; b++){        returnString = decodeText.replace(filterEncode[a],filterDecode[a]);        decodeText = returnString;      }    }    return returnString;  },  encode : function(encodeText){    let returnString, a, b;    let filterEncode = Url.filterEncode;    let filterDecode = Url.filterDecode;    for (a = 0; a < filterEncode.length; a++){      let regexEn = new RegExp("[" + filterEncode[2] + "]","g");      let regexDe = new RegExp("[" + filterDecode[2] + "]","g");      let regex = new RegExp(regexEn,"g");      let array = (encodeText.match(regex) || []).length;      for (b = 0; b < array; b++){        returnString = encodeText.replace(filterDecode[a],filterEncode[a]);        encodeText = returnString;      }    }    return returnString;  }}// Saying it is undefinedconsole.log(Url.encode("="));// Encodes it just findconsole.log(Url.encode("%3F"));// Encodes both of them but for some odd reason encodes the// equal sign twice.console.log(Url.encode("%3F ="));我确实希望关于我的问题是什么,一切看起来都很清楚。我通常只是尝试在这里搜索答案,但这个问题太混乱了,我不知道我到底应该搜索什么。
查看完整描述

1 回答

?
皈依舞

TA贡献1851条经验 获得超3个赞

您的某些字符串filterDecode作为正则表达式具有特殊含义。将字符串转换为正则表达式时,需要将每个字符都包裹起来,[]以便按字面意思进行匹配。


/创建regexDe. _


不需要for(b...)循环。在调用中使用正则表达式,replace()它将立即执行所有替换,因为它有g标志。


将包含的编码字符串放在%数组的开头。否则,当您对类似=as的内容进行编码时%3D,外循环的后续迭代会将其重新编码为%253D。如果它在原始字符串中,而不是中间步骤,您只想对其进行编码。


var Url = {

  filterDecode: ["%252F", "%253A", "%253D", "%253F", "%252B", "%2B", "%3A", "%3D", "%3F", "%2F", "%26"],

  filterEncode: ["%2F", "%3A", "%3D", "%3F", "%2B", "+", ":", "=", "?", "/", "&"],

  strToRe: function(str) {

    let reStr = str.split("").map(c => '[' + c + ']').join('');

    return new RegExp(reStr, "g");

  },

  decode: function(decodeText) {

    let a;

    let filterEncode = Url.filterEncode;

    let filterDecode = Url.filterDecode;

    for (a = 0; a < filterDecode.length; a++) {

      decodeText = decodeText.replace(Url.strToRe(filterDecode[a]), filterEncode[a]);

    }

    return decodeText;

  },

  encode: function(encodeText) {

    let a, b;

    let filterEncode = Url.filterEncode;

    let filterDecode = Url.filterDecode;

    for (a = 0; a < filterEncode.length; a++) {

      encodeText = encodeText.replace(Url.strToRe(filterEncode[a]), filterDecode[a]);

    }

    return encodeText;

  }

}


console.log(Url.encode("="));

console.log(Url.decode("%3D"));

console.log(Url.encode("%3F"));

console.log(Url.decode("%253F"));

console.log(Url.encode("%3F ="));

console.log(Url.decode("%253F %3D"));


查看完整回答
反对 回复 2022-12-22
  • 1 回答
  • 0 关注
  • 146 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信