有沒有懶惰的方式來獲得「頂級」主機的變量,而不訴諸if()?Javascript返回只是主域名
- example.com:返回example.com,
- cabbages.example.com:返回example.com,
- carrots.example.com:返回example.com,
- otherexample.com:返回otherexample.com,
- cabbages.otherexample.com:返回otherexample.com,
- carots.otherexample.com:返回otherexample.com,
您可以嘗試[正則表達式](http://eloquentjavascript.net/09_regexp.html)或[String.prototype.split](https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/String/split) – nils
'cabbages.example.co.uk'怎麼樣?這樣做需要知道每個頂級域名的命名約定。 – Barmar