c00000fd
c00000fd

Reputation: 22275

RegEx match for a symbol at the beginning of a string, or the beginning of string itself

I'm trying to come up with a regex that would catch a TLD in a group of subdomains. So I have this:

/\.?google\.com$/i

That should do the following matches:

sgoogle.com            -- no match
google.com             -- match
maps.google.com        -- match
job_s.map.google.com   -- match
place.google.com.eu    -- no match

This works, except for the first one, i.e. sgoogle.com that it matches as well, which it shouldn't.

So I'm curious, is there a way to specify where I do \.? to match either a dot or the beginning of string?

Upvotes: 0

Views: 50

Answers (1)

Lucas Trzesniewski
Lucas Trzesniewski

Reputation: 51330

You're almost there. Literally, your requirement would be:

(?:^|\.)google\.com$

But depending on your needs, this could be better (or not, you tell me):

\bgoogle\.com$

The \b means word boundary.

Upvotes: 5

Related Questions