Given URLs like:
http://online.wsj.com/
http://online.wsj.com/article/SB10001424052970204409004577158764211274708.html
http://www.techcrunch.com/2012/01/13/techcrunch-coo/
Using Ruby/Rails, how can I return back just the domain?
online.wsj.com
online.wsj.com
techcrunch.com
No protocol, no slashes, just the subdomain if it's not www
, and the domain, and ext?
Any name registered in the DNS is a domain name. To extract a domain name from a URL, first remove the protocol (http://, https://, ftp://, etc.), then remove any subdomains (www., blog., etc.), then remove the top-level domain (.com, . net, . org, etc.).
The =REGEXREPLACE() function is built-in Google Sheets and it extracts domains from URLs. What's great about is it's only a simple line of code that you can paste into your cell. The function is not super technical and you can change it any way you see fit.
First let's create a string with our URL (Note: If the URL isn't correctly structured you'll get an error). const url = 'https://www.michaelburrows.xyz/blog?search=hello&world'; Next we create a URL object using the new URL() constructor. let domain = (new URL(url));
Use Addressable::URI.parse and the #host instance method:
Addressable::URI.parse("http://techcrunch.com/foo/bar").host #=> "techcrunch.com"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With