1

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"

liakwee
  • 33
  • 1
  • 6
  • 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 Answers5

7

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
Juan Pablo Califano
  • 12,073
  • 5
  • 28
  • 41
2

This should do the work: http(s?)://([\w]+\.){1}([\w]+\.?)+ You can try this in GSkinner RegExr

OXMO456
  • 3,568
  • 2
  • 24
  • 35
1

Try this one:

.*\/\/([^\/:]+).*

Surrounds the regexp by the preferred separators, which might depend on language. This diagram explain how it works.

Eugen Mihailescu
  • 3,268
  • 2
  • 28
  • 28
0

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.

JsAndDotNet
  • 15,064
  • 14
  • 93
  • 118
0

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