-2

I have certain URLs, for example:

http://www.moneycontrol.com/news/business/vistaras-turbulent-takeoffthe-cut-throat-indian-skies_3510081.html
http://www.business-standard.com/article/markets/patanjali-ayurved-targets-250-revenue-growth-in-fy16-edelweiss-115100900788_1.html

These can be any website URL. How can I extract www.moneycontrol.com or www.business-standard.com from the content?

Richard Hamilton
  • 25,478
  • 10
  • 60
  • 87
Pawan
  • 31,545
  • 102
  • 256
  • 434
  • Have a look at https://gist.github.com/acdcjunior/9820040 : `var myDomain = new ParsedUrl("http://www.moneycontrol.com/news/business/vistaras-turbulent-takeoffthe-cut-throat-indian-skies_3510081.html").host;` – acdcjunior Oct 09 '15 at 15:02
  • Here is a version not using split: `var domains = urls.map(function(url) { var a = document.createElement("a"); a.href=url; return a.hostname; });` – mplungjan Oct 09 '15 at 15:10

2 Answers2

1

You basically need the domain from the url. You can get it this way:

function extractDomain(url) {
    var domain;

    if (url.indexOf("://") > -1) 
        domain = url.split('/')[2];
    else 
        domain = url.split('/')[0];

    return domain.split(':')[0];
}
divoom12
  • 802
  • 4
  • 12
1

This can easily be done with two split functions to split a string into an array.

var path = string.split("://")[1].split('/')[0]

this gives you a URL like www.moneypath.com.

If you want to get just the url name, by eliminating www. and .com, either of these 2 will work.

path.replace(/(www.)|(.com)/g, '')

You can also use the split approach again

path.split('www.')[1].split('.com')[0]

Personally, I prefer the Regex approach since it's a little cleaner.

Richard Hamilton
  • 25,478
  • 10
  • 60
  • 87