我想要一個機制從location.hostname
中提取子域,這應該足以滿足以下所有情況。從主機名提取子域
1. example.com => return value is blank since no sub domain
2. www.example.com => return value is blank since no sub domain
3. test.example.com => return value should be test since this is the sub domain
4. example.co.in => return value is blank since no sub domain
5. www.example.co.in => return value is blank since no sub domain
6. test.example.co.in => return value should be test since this is the sub domain
7. 183.87.46.82 => return value is blank since IP passed
僅針對上述給定的場景,我需要處理。我不期待這件事。最重要的是,我不需要提取任何嵌套的子域名,只有第一級子域名就足夠了。
在這方面的任何想法都會有所幫助。
如下回答可以幫助你 - HTTP: //stackoverflow.com/a/23945027/2020893 –
這也是 - https://css-tricks.com/snippets/javascript/get-url-and-url-parts-in-javascript/ –
投票結束,因爲SO不是代碼編寫服務。 – RobG