有没有懒惰的方式来获得“顶级”主机的变量,而不诉诸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