Link to home
Start Free TrialLog in
Avatar of MJ
MJFlag for United States of America

asked on

Regex for hostname

I have the following code:
if (currHost.match(/(alpha|beta|test|dev|load|local)\./)) {

Open in new window

but I need to add additional conditions such as "file:" , ".od*." (* is wildcard), "dev-wa." and ".hbox."  not sure how to approach this?
Avatar of kaufmed
kaufmed
Flag of United States of America image

Your current regex will match those various words if they occur *anywhere* in the string contained within currHost. Your inclusion of "file:" seems to imply that you want to include a scheme as well. Is this correct? If you can clarify what "things" you want to be matched, then we can appropriately place them in your regex.
Avatar of MJ

ASKER

Capturing the URL (without query string) via
 var currHost = location.protocol+"//"+location.hostname+location.pathname;

Open in new window

and want to filter out non-production urls coming for our analytics code. So we could have something with
 file://
www.sqc.mydomain.com
www.sqa.mydomain.com
www.odd.mydomain.com
www.odp.mydomain.com
www.hbox.mydomain.com
dev-wa.mydomain.com

etc
Avatar of MJ

ASKER

Anybody out there? Where's emoticons when you need them!!!
ASKER CERTIFIED SOLUTION
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MJ

ASKER

Below is the solution:
if ((currHost[i].match(/(alpha|beta|test|dev|load|local|phi|\.stage|\.stg|\.it.{1}|\.od.{1}|dev-wa|\.sq.{1}|\.hbox)\./))||(document.location.protocol === 'file:')) {

Open in new window