liakwee
liakwee

Reputation: 33

Regexp to get domain from URL

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"

Upvotes: 1

Views: 10023

Answers (5)

Arunit Mazumdar
Arunit Mazumdar

Reputation: 133

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

Upvotes: 0

Eugen Mihailescu
Eugen Mihailescu

Reputation: 3711

Try this one:

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

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

Upvotes: 1

JJJCoder
JJJCoder

Reputation: 16936

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.

Upvotes: 0

OXMO456
OXMO456

Reputation: 3548

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

Upvotes: 2

Juan Pablo Califano
Juan Pablo Califano

Reputation: 12333

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

Upvotes: 6

Related Questions