Anyone knows the Regexp for AS3 to get the domain name from URL? E.g: "http://www.google.com/translate" will get "http://www.google.com"
-
If you don't care about TLD with double endings (co.uk, co.jp etc) then check http://stackoverflow.com/questions/863297/regular-expression-to-retrieve-domain-tld or google for "RegExp tld" – Mikael Svenson Aug 07 '10 at 18:29
5 Answers
There's is a very complete utility class for dealing with URIs in as3corelib. Perhaps you might want to use this instead of rolling your own.
import com.adobe.net.URI;
var uri:URI = new URI("http://www.google.com/translate");
trace(uri.authority); // traces www.google.com

- 12,213
- 5
- 29
- 42
Try this one:
.*\/\/([^\/:]+).*
Surrounds the regexp by the preferred separators, which might depend on language. This diagram explain how it works.

- 3,553
- 2
- 32
- 29
The previous regex provided missed some url types i needed, so for the benefit of googlers, I use
(http://|https://)?(www.)?[A-z]*(.com|.co.uk|.us|.org|.net|.mobi)
in C#
var regexMatch = Regex.Match(input, @"(http://|https://)?(www.)?[A-z]*(.com|.co.uk|.us|.org|.net|.mobi)", RegexOptions.Multiline);
string domain = regexMatch.Value;
See example at RegExr.
You can remove remove "(...)?" parts to stop that part appearing the match, for example to remove the http matching that I require.
Probably not perfect, but works for me. This site is an excellent reference for building up expressions.

- 16,260
- 18
- 100
- 123
Regex to get the domain from the window.location.href property is provided below. Modifications have to made to a regex answer provided previously by OXMO456 to make it correct and accommodate more scenarios.
domainRegex = /(http(?:s?):\/\/(?:[\w]+(?:\.|\:)){1}(?:[\w]+\.?)+)/gi;
Regexr link: https://regexr.com/4vnnc

- 113
- 9