0

I have just started to use regular expression and I am finding it difficult to find a regular expression to get the main domain name, that is, if I have urls as given below..

url = http://blog.example.co.in/page/category=?order.php
url = https://www.example.co.ca?page.html

so out of this I want to get only the example out the urls given above. So how can i do it..!!If you can get example in any other method will also be helpful..

I myself tried some methods as given below..

var url = urls.replace('http://','').replace('https://','').replace('www.','').split(/[/?#]/)[0];
var out = url.split('.')[0];

in this I am able to get the output for

https://www.example.co.ca?page.html

but not for

url = http://blog.example.co.in/page/category=?order.php

so can someone help me out a method that can get example out of any kind url

thank you

Amie james
  • 103
  • 2
  • 8
  • 2
    Why use a regular expression http://stackoverflow.com/questions/736513/how-do-i-parse-a-url-into-hostname-and-path-in-javascript – epascarello Jan 06 '15 at 18:04
  • "example" isn't the domain name. "example.co.in" or "example.co.ca" would be the domain name. – vintastic Jan 06 '15 at 18:22
  • yea i know that..so can you atleast tell me how to get example.co.in or example.com.ca from the above urls – Amie james Jan 06 '15 at 18:26
  • http://regexr.com/39cuo. RegExr is a great (FREE) resource and tester. I personally use a paid app from http://www.regexbuddy.com/, but he's got lots of great, free, tutorial resources at http://www.regular-expressions.info/. – Phil Tune Jan 06 '15 at 20:02
  • The issue with this example - getting the "main" domain when your top-level domain (TLD) has more than one "part" (don't know the technical terms) is you would need to have a list of all those types of TLDs so you're not just grabbing `/\.whatever$/` plus whatever is immediately before it. Your RegEx will have a hard time distinguishing if your domain is "co.in", "example.co.in" or "blog.example.co.in". That's why [this makes more sense](http://stackoverflow.com/a/736970/1128978) than using RegEx. – Phil Tune Jan 06 '15 at 20:13

0 Answers0