-1

Example urls:

  • let str = 'https://www.google.com.ua/images/branding/product/ico/googleg_lodp.ico'
  • let str1 = 'https://www.google.com/images/branding/product/ico/googleg_lodp.ico'
  • let str2 = 'https://books.google.com.ua/images/branding/product/ico/googleg_lodp.ico'

How can I get only the site name from such urls? Only the word - google.

Barmar
  • 741,623
  • 53
  • 500
  • 612
VINET
  • 641
  • 1
  • 7
  • 28

2 Answers2

0

The following pattern will return the group name with what you want:

[.+:\/\/]?[^.]*\.?(?<name>[^.]+).+

Working demo

It will work in cases of urls like https://google.com.br, for example.Let me know if you think this way is sufficient.

EDIT:

It is very difficult to treat cases like I pointed:

  • books.google.com
  • books.londrina.br

In the first case, the correct return would be google, but in the second case the desirable result is books.

Nizam
  • 4,569
  • 3
  • 43
  • 60
  • this does not detract from the subdomain, if I understand correctly. Can make of this js function? – VINET Aug 25 '17 at 20:16
  • Look at https://stackoverflow.com/questions/6603015/check-whether-a-string-matches-a-regex. – Nizam Aug 25 '17 at 20:25
0

This works reasonably well:

https?.*?(\w{4,})(?=(?:\.\w{2,3})+\/)

The name is in group 1.

Demo. It's likely better to use a whitelist of TLDs.

linden2015
  • 887
  • 7
  • 9