问题
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 stringhttps?:\/\/- ahttps://orhttp://substring(?:[^\/]+\.)?- an optional sequence of 1 or more chars (as many as possible) other than/followed with a.mysite\.com- a literalmysite.comsubstring(?:\/(?:\?.*)?)?- 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