小编Sam*_*iel的帖子

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

嗨,我正在尝试匹配允许查询字符串的特定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)

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

javascript regex

6
推荐指数
1
解决办法
2万
查看次数

标签 统计

javascript ×1

regex ×1