【问题标题】:Merge multiple URL checks into one statement (JavaScript)将多个 URL 检查合并到一个语句中 (JavaScript)
【发布时间】:2018-11-22 17:45:50
【问题描述】:

例子:

if (/entertainments/.test(window.location.href)) { //这里的代码 } if (/suggestions/.test(window.location.href)) { //这里的代码相同 }

我试过了

如果 (/entertainments/||/suggestions/.test(window.location.href))

【问题讨论】:

    标签: javascript href window.location


    【解决方案1】:

    请尝试:

    if (/entertainments/.test(window.location.href) || /suggestions/.test(window.location.href)) {
        //code here
        console.log("Matches the req...");
    }
    

    当您将其中一个设置为 true 时,将执行 if-body(在本例中为 console.log("Matches the req...");

    【讨论】:

    • 谢谢你,这非常有效,非常有意义。
    【解决方案2】:

    试试

    if(/entertainments/.test(window.location.href)||/suggestions/.test(window.location.href) )
    

    【讨论】:

    • 请解释这里发生了什么。
    【解决方案3】:

    将您的正则表达式更改为此

    /^(entertainment|suggestion)/.test(window.location.href);
    

    【讨论】:

      猜你喜欢
      • 2014-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多