match home page url with regex

荒凉一梦 提交于 2021-01-28 14:17:48

问题


I want to match only the homepage of website www.mysite.com and any variations of it like mysite.com/?src=advert

I have come up with ^https?:\/\/[^\/]+(\/(\?.*|index\.com(\?)?)?)?$

It matches www.mysite.com but I need it to match anything after /? as well www.mysite.com/? (and anything after question mark)

I don't want to match mysite.com/product/..


回答1:


It seems you are looking for

^https?:\/\/(?:[^\/]+\.)?mysite\.com(?:\/(?:\?.*)?)?$

See the regex demo

Details

  • ^ - start of the string
  • https?:\/\/ - a https:// or http:// substring
  • (?:[^\/]+\.)? - an optional sequence of 1 or more chars (as many as possible) other than / followed with a .
  • mysite\.com - a literal mysite.com substring
  • (?:\/(?:\?.*)?)? - an optional sequence of:
    • \/ - a /
    • (?:\?.*)? - an optional sequence of ? and then any 0+ chars other than line break chars, as many as possible
  • $ - end of string


来源:https://stackoverflow.com/questions/46320439/match-home-page-url-with-regex

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!