将多个URL检查合并为一个语句(JavaScript)

时间:2018-06-13 08:12:57

标签: javascript href window.location

示例:


    if (/entertainments/.test(window.location.href)) {
    //code here

}


       if (/suggestions/.test(window.location.href)) {
    //same code here

  }

我试过了


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

3 个答案:

答案 0 :(得分:1)

请尝试:

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

当你将其中一个变为真时,if-body将被执行(在这种情况下为console.log("Matches the req...");

答案 1 :(得分:1)

尝试

dz

答案 2 :(得分:0)

将正则表达式更改为此

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