【问题标题】: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);