【问题标题】:Decode (loop) until string URI is the same解码(循环)直到字符串 URI 相同
【发布时间】: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


【解决方案1】:

您正在尝试递归解码一些encoded URI,直到解码它不再改变result

如果我的理解是正确的,请看下面:

/* Suppose our sample case is a recursively encoded URI */

let URIEncoded = "https%3A%2F%2Fw3schools.com%2Fmy%20test.asp%3Fname%3Dst%C3%A5le%26car%3Dsaab_VERY_LONG_URL"
console.log('URI ENCODED ONCE:', URIEncoded)

let URIEncodedTwice = encodeURIComponent(URIEncoded)
console.log('URI ENCODED TWICE:', URIEncodedTwice)

let URIEncodedThrice = encodeURIComponent(URIEncodedTwice)
console.log('URI ENCODED THRICE:', URIEncodedThrice)


/* Get the original URI */
let baseURI = decodeNestedURI(URIEncodedThrice);
console.log('BASE URI:', baseURI) // print result

/* function decodeNestedURI returns the base URI of one that's been encoded multiple times */
function decodeNestedURI(nestedURI) {
    let oldURI = nestedURI
    let newURI = null
    let tries = 0

    while (true) {
      tries++
      newURI = decodeURIComponent(oldURI);
      
      if (newURI == oldURI) break // quit when decoding didn't change anything
      
      oldURI = newURI
    } 
    console.log('TIMES DECODED:', tries-1) // -1 because the last decoding didn't change anything
    return newURI 
}

希望这会有所帮助。 干杯,

【讨论】:

  • 太好了,非常感谢 jonathangersam!有用。这对我来说是一个很好的例子,可以让我继续处理我的代码。这对我真的很有帮助,因为我是 JS 的新手。只是一个简短的问题:是否有特殊原因,为什么使用“let”而不是“var”?谢谢,祝您有愉快的一天!
  • 很高兴为您提供帮助 :) 从技术上讲,“var”是函数范围的,而“let”是块范围的。 'let' 本质上是避免原始 javascript 'var' 作为包袱的范围问题的最佳实践。更详细的,推荐扫一扫本章exploringjs.com/es6/ch_variables.html
猜你喜欢
  • 1970-01-01
  • 2011-06-18
  • 2014-06-28
  • 1970-01-01
  • 2012-01-05
  • 1970-01-01
  • 2011-02-07
  • 2018-05-13
  • 2016-06-18
相关资源
最近更新 更多