正则表达式将特定URL与查询字符串匹配

Sam*_*iel 6 javascript regex

嗨,我正在尝试匹配允许查询字符串的特定URL.基本上我需要发生以下事情:

  • http://some.test.domain.com - 通过
  • http://some.test.domain.com/ - 通过
  • http://some.test.domain.com/home - 通过
  • http://some.test.domain.com/?id=999 - 通过
  • http://some.test.domain.com/home?id=888&rt=000 - 通过
  • http://some.test.domain.com/other - 失败
  • http://some.test.domain.com/another?id=999 - 失败

这是我到目前为止:

var pattern = new RegExp('^(https?:\/\/some\.test\.domain\.com(\/{0,1}|\/home{0,1}))$');
if (pattern.test(window.location.href)){
    console.log('yes');   
}
Run Code Online (Sandbox Code Playgroud)

上面的代码仅适用于前三个而不适用于查询字符串.任何帮助,将不胜感激.谢谢.

p.s*_*w.g 5

这样的模式应该有效(至少对于您的特定域)

/^http:\/\/some\.test\.domain\.com(\/(home)?(\?.*)?)?$/
Run Code Online (Sandbox Code Playgroud)

这将匹配一个http://some.test.domain.com可选的文本/,后跟所有的文本,可选的后跟一个文本home,可选的后跟一个文本?和任意数量的其他字符。

你可以在这里测试