What need a template RegEx to get a domain name.
For example, i have:
lifenews.ru
www.forbes.com
goo.gl
ya.ru
and get:
lifenews
forbes
goo
ya
What need a template RegEx to get a domain name.
For example, i have:
lifenews.ru
www.forbes.com
goo.gl
ya.ru
and get:
lifenews
forbes
goo
ya
How about:
^(?:.*(?=\..*\..*).)?([^.]+)
The first part ((?:.*(?=\..*\..*).)?
) will consume everything before (a sequence of words with two dots, like) abc.com
in www.stuff.abc.com
. The next part (([^.]+)
) matches abc
.
Javascript w/o regex:
var parts = "www.forbes.com".split('.');
parts.pop();//first level domain
var domain = parts.pop(); //second level domain
preg_match('/(?:www\.)?(\w+)\./', $url, $matches);
$host = $matches[1];
This works and, as far as I can tell, it accounts for someone putting www
or not.