问题
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"
回答1:
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
回答2:
This should do the work: http(s?)://([\w]+\.){1}([\w]+\.?)+
You can try this in GSkinner RegExr
回答3:
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.
回答4:
Try this one:
.*\/\/([^\/:]+).*
Surrounds the regexp by the preferred separators, which might depend on language. This diagram explain how it works.
来源:https://stackoverflow.com/questions/3431523/regexp-to-get-domain-from-url