Reputation: 63
I'm trying to create a regex that will optionally match any subdomains, and match the TLD. For example, it should match…
It should not match…
I have this so far, which matches subdomains, but does not match when at the top level domain.
(\A|(https?:\/\/))?(\w*|\S*)\.{1}example\.com
Upvotes: 1
Views: 667
Reputation: 627190
You may use
/\A(?:https?:\/\/)?(?:\S*\.)?example\.com\z/
See the regex demo
Details
\A
- start of string(?:https?:\/\/)?
- an optional (as the ?
quantifier at the end repeats 1 or 0 times) non-capturing group matching http
, an optional s
and then //
substring(?:\S*\.)?
- an optional non-capturing group matching 1 or 0 occurrences of 0 or more non-whitespace chars (with \S*
) and then a dot (\.
)example\.com
- an example.com
substring\z
- end of string.Upvotes: 3