开发者

How to write regex to match pages and subdomains of one URL only?

开发者 https://www.devze.com 2023-02-27 02:12 出处:网络
I\'m trying to get the following into a regex: *.foo.com/* success test cases www.foo.com www.foo.com/blah

I'm trying to get the following into a regex:

*.foo.com/*

success test cases

  • www.foo.com
  • www.foo.com/blah
  • aaa.www.foo.com/woot/woot/woot

failure cases

How would I write a regex to do this?


A literal translation of your glob into regex is:

.*\.foo\.com(/.*)?


This should work, if you mean to test the sub-domains for foo.com:

/(?<part>(w(?!ww)|w(?=[a-z0-9]{3,})|[a-vx-z0-9])[a-z0-9]*)\.foo\.com/


I have an easy regex that works:

(.*)\.foo\.com

You can easily test a regular expression yourself using rubular. I strongly recommend it.

0

精彩评论

暂无评论...
验证码 换一张
取 消