我正在研究一个 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 ="));
我确实希望关于我的问题是什么,一切看起来都很清楚。我通常只是尝试在这里搜索答案,但这个问题太混乱了,我不知道我到底应该搜索什么。
皈依舞
相关分类