代码之家  ›  专栏  ›  技术社区  ›  David

如何相位?如果变量=(条件1)+(任何内容)

  •  0
  • David  · 技术社区  · 15 年前

    我该怎么写这个 if statement 在javascript中?

    if(url == "http://www.google.com/" && "*")
      { ... }
    

    这个 * * ,条件必须仍然是真的。。。

    2 回复  |  直到 15 年前
        1
  •  3
  •   Nick Craver    15 年前

    你可以用 .indexOf() ,如下所示:

    if(url.indexOf("http://www.google.com/") == 0)
    

    它的意思是“网址 http://www.google.com/ ". 或者根据您的目标,regex可能更好,例如:

    if(/^http:\/\/www.google.com\//​​​​​​​​.test(url))
    
        2
  •  2
  •   Gumbo    15 年前

    你可以用这个 startsWith

    String.prototype.startsWith = function(prefix) {
        return this.substr(0, prefix.length) === prefix;
    };
    

    然后你可以在 url 这样地:

    if (url.startsWith("http://www.google.com/"))