Doug Fir
Doug Fir

Reputation: 21242

regex for several domains

I am using a regular expression to determine when to fire a tracking tag or not.

If a visitor to one of the sites is on one of these three domains the tag should fire:

 - www.grousemountainlodge.com 
 - www.glacierparkinc.com
 - reserveglacierdenali.com

I actually have a regular expression that works. But I'm not confident and wanted to bounce it off the folk on this board.

This is what I have. Is there a simpler, more elegant or more robust regex to use for matching the 3 domains?

^(www\.)?((glacierparkinc|grousemountainlodge)\.com)$|(^reserveglacierdenali\.com)$

Following some answers, this regex should exlude other domains e.g. cats.glacierparkinc.com or similar.

Upvotes: 0

Views: 192

Answers (2)

user4227915
user4227915

Reputation:

It must be at starting position with or not www.. So:

^(?:www\.)?(?:glacierparkinc|grousemountainlodge|reserveglacierdenali)\.

If it maches, then do something.

Regex live here.

Hope it helps.

Upvotes: 1

shevron
shevron

Reputation: 3663

I'm not sure whether glacierparkinc.com should match, without the www. prefix - from your list it seems that no, but from your regex it seems it will be matched.

In either case I guess you can simplify it a bit:

^(?:www\.(?:glacierparkinc|grousemountainlodge)|reserveglacierdenali)\.com$

Note the use of (?:) instead of just (): this means positive look-ahead assertion without capturing. Its a best practice not to capture when you don't need to - saving time and memory.

Upvotes: 1

Related Questions