【发布时间】:2018-10-05 11:47:45
【问题描述】:
我想解码字符串 URI,直到没有变化。 通常字符串 URI 有大约 53'000 个字符。所以比较应该很快。在我的示例代码中,我使用了字符串的简短形式。
这是我的示例代码,不幸的是它不起作用:
var uri = "https%3A%2F%2Fw3schools.com%2Fmy%20test.asp%3Fname%3Dst%C3%A5le%26car%3Dsaab_VERY_LONG_URL"
var firstDecode = decodeURIComponent(uri.replace(/\+/g, " "));
var res = Decode(firstDecode);
function Decode(firstDecode){
var secondDecode = decodeURIComponent(uri.replace(/\+/g, " "))
while (firstDecode.localeCompare(secondDecode) != 0) {
firstDecode = decodeURIComponent(uri.replace(/\+/g, " "))
}
return firstDecode;
}
/* Expected Returns:
localeCompare()
0: exact match
-1: string_a < string_b
1: string_a > string_b
*/
我怎样才能以最流畅的方式做到这一点? 提前致谢。
更新 1
好的,我的代码的新版本:
var uri = "https%3A%2F%2Fw3schools.com%2Fmy%20test.asp%3Fname%3Dst%C3%A5le%26car%3Dsaab_VERY_LONG_URL"
var res = Decode(uri);
function Decode(uri){
var initialURI = URI
var newURI = decodeURIComponent(uri.replace(/\+/g, " "));
If (initialURI === newURI) {
// no changes anymore
return newURI;
} else {
// changes were detected, do this function again
var res = Decode(newURI);
}
}
但它仍然无法正常工作。
【问题讨论】:
-
您正在尝试解码 URI?如果它可能是您正在寻找的东西,请尝试这个 SO 答案stackoverflow.com/a/13691499/6717375
-
@jonathanangersam 感谢您的链接。解码 URI 不是问题。问题是,如何实现解码后的 URI 与之前的 URI 相同,然后停止执行。因为一个包含 53000 个字符的 URI 需要多次解码。
-
如何实现解码后的 URI 相同——在你的 while 循环条件下,(1)考虑使用
localeCompare(本地e 带有 e); (2) 编辑条件,因为当前条件总是评估为0(匹配),因为它就像说'hello'.localeCompare('hello')。 -
@jonathanangersam 感谢您的提示。我已经更新了我的问题,新的尝试。
标签: javascript loops uri decode