0

I need to know if a string contains a specific domain

I have an array like this

private var validDomain:Array  =  new Array(
  "http://*.site1.com",
  "http://*.site2.com",
  "http://*.site3.com",
  );

private var isValidDomain:Boolean =  false; 
private var URL:string =  "http://mysub.site2.com";  

now i would check if my string is valid, so i think something like that:

for each (var domain_ in validDomain){
      if(SOMEREGEX){
      isValidDomain=true;
      }
  }

What i put in SOMEREGEX?!

mikmprdd
  • 411
  • 1
  • 4
  • 13
  • possible duplicate of [Reference - What does this regex mean?](http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) – Heretic Monkey Jan 19 '15 at 16:51

1 Answers1

0

The problem lies in the fact that you use two different logics.

The first one is a wildcard-based string, with wildcards like *; you must translate this wildcard based pattern in a regular expression pattern.

To accomplish this, a quick and dirty solution would be to do some string replacements like:

  • substituting the * wildcard in the pattern with .*

  • escaping the characters in the wildcard based pattern that are "special" to regular expressions (e.g. substituting . with \.

With this logic in mind, you will transform a wildcard based pattern like:

http://*.mydomain.com

into a regular expression pattern:

http:\/\/.*\.mydomain\.com

which you can use to test your string.

edit: .* is a very crude way to test for a subdomain, to do things neatly you should use a correct pattern like the ones in this thread: Regexp for subdomain

Community
  • 1
  • 1
Cranio
  • 8,964
  • 2
  • 29
  • 51