无法正确解码/编码 Url

我正在研究一个 Url 解码和编码系统。但由于一些奇怪的原因,它只会解码/编码特定的字符串。此外,它似乎在字符串中包含特定片段时对其进行解码/编码。这很难解释,但对于这可能行不通的事情却很困惑。我试着弄清楚它的问题,但这只会让整个问题看起来更不合逻辑。


我希望有人可以帮助我。有解释。如果代码与它的风格相同,我会喜欢它。


我也知道网上可能有一些软件包可以轻松做到这一点,但我宁愿自己制作。这是我可以更多地练习 JS 的一种方式。


// I know I don't have all the characters markek. I am doing that later

var 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 undefined

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


// Encodes it just find

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


// Encodes both of them but for some odd reason encodes the

// equal sign twice.

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

我确实希望关于我的问题是什么,一切看起来都很清楚。我通常只是尝试在这里搜索答案,但这个问题太混乱了,我不知道我到底应该搜索什么。



慕少森
浏览 147回答 1
1回答

皈依舞

您的某些字符串filterDecode作为正则表达式具有特殊含义。将字符串转换为正则表达式时,需要将每个字符都包裹起来,[]以便按字面意思进行匹配。/创建regexDe. _不需要for(b...)循环。在调用中使用正则表达式,replace()它将立即执行所有替换,因为它有g标志。将包含的编码字符串放在%数组的开头。否则,当您对类似=as的内容进行编码时%3D,外循环的后续迭代会将其重新编码为%253D。如果它在原始字符串中,而不是中间步骤,您只想对其进行编码。var Url = {&nbsp; filterDecode: ["%252F", "%253A", "%253D", "%253F", "%252B", "%2B", "%3A", "%3D", "%3F", "%2F", "%26"],&nbsp; filterEncode: ["%2F", "%3A", "%3D", "%3F", "%2B", "+", ":", "=", "?", "/", "&"],&nbsp; strToRe: function(str) {&nbsp; &nbsp; let reStr = str.split("").map(c => '[' + c + ']').join('');&nbsp; &nbsp; return new RegExp(reStr, "g");&nbsp; },&nbsp; decode: function(decodeText) {&nbsp; &nbsp; let a;&nbsp; &nbsp; let filterEncode = Url.filterEncode;&nbsp; &nbsp; let filterDecode = Url.filterDecode;&nbsp; &nbsp; for (a = 0; a < filterDecode.length; a++) {&nbsp; &nbsp; &nbsp; decodeText = decodeText.replace(Url.strToRe(filterDecode[a]), filterEncode[a]);&nbsp; &nbsp; }&nbsp; &nbsp; return decodeText;&nbsp; },&nbsp; encode: function(encodeText) {&nbsp; &nbsp; let a, b;&nbsp; &nbsp; let filterEncode = Url.filterEncode;&nbsp; &nbsp; let filterDecode = Url.filterDecode;&nbsp; &nbsp; for (a = 0; a < filterEncode.length; a++) {&nbsp; &nbsp; &nbsp; encodeText = encodeText.replace(Url.strToRe(filterEncode[a]), filterDecode[a]);&nbsp; &nbsp; }&nbsp; &nbsp; return encodeText;&nbsp; }}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"));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript