0

I have just started using regular expression and i landed up in a problem. So it would be really nice if someone can help me out with it.

The problem is, in case I have a url as given below;

$url = http://www.blog.domain.com/page/category=?

and want only the domain, how can i get it using regular expression in javascript.

thank you

1
  • Please find the answers below and mark one as accepted that solves your problem. Commented Jan 6, 2015 at 6:57

4 Answers 4

1

This should work too, but most restrictive and shorter:

var url = "http://www.blog.domain.com/page/category"
var result = url.replace(/^(https?:\/\/)?(.+\.)*(([a-z0-9-]*)\.[a-z]{2,6})(\/.+)$/i,"$4")

If you want "domain.com" and not only "domain", use $3 instead of $4.

Explaination step by step:

  • A correct domain syntax: letters,numbers and "-" /([a-z0-9-]*)/i
  • Domain extension (2-6 chars): /(([a-z0-9-]*)\.[a-z]{2,6})/i
  • Subdomains: /(.+\.)*(([a-z0-9-]*)\.[a-z]{2,6})/i
  • An url start with http and maybe https: /^https?:\/\/(.+\.)*(([a-z0-9-]*)\.[a-z]{2,6})/i
  • You can put or not http when you type an url: /^(https?:\/\/)?(.+\.)*(([a-z0-9-]*)\.[a-z]{2,6})/i
  • Then what is after /: /^(https?:\/\/)?(.+\.)*(([a-z0-9-]*)\.[a-z]{2,6})(\/.+)$/i
Sign up to request clarification or add additional context in comments.

Comments

0

Try below code

 var url = "http://www.blog.domain.com/page/category=?";
 var match = url .match(/(?:http?:\/\/)?(?:www\.)?(.*?)\//);
 console.log(match[match.length-1]);

1 Comment

but what if i have a url like blog.domain.com/page/category=?product.asp. In this case i would be getting product and not domain
0

You can get it using the following RegEx: /.*\.(.+)\.[com|org|gov]/

You can add all of the supported domain extensions in this regex.

RegEx101 Explanation

Working Code Snippet:

var url = "http://www.blog.domain.gov/page/category=?";

var regEx = /.*\.(.+)\.[com|org|gov]/;

alert(url.match(regEx)[1]);

1 Comment

bro all urls do not end in .com
0

Do not use regex for this:

use hostname:

The URLUtils.hostname property is a DOMString containing the domain of the URL.

var x = new URL("http://www.blog.domain.com/page/category=?").hostname;
console.log(x);

as pointed by vishwanath, URL faces compatibilty issues with IE<10 so for those cases, regex will be needed.

use this :

var str = "http://www.blog.domain.com/page/category=?";
var res = str.match(/[^.]*.(com|net|org|info|coop|int|co\.uk|org\.uk|ac\.uk|uk)/g);
console.log(res);

=> domain.com

the list in the regex can be expanded further depending upon your need. a list of TLDs can be found here

4 Comments

I tried this but it gave me www.blog.domain.com but whereas i need only domain out of it.
@AmandaWalker The domain part by itself is not a domain name.
then which part is the domain ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.