Reputation: 1199
URL Examples:
http://www.demotest.com/categoey/t23
http://demotest.com/categoey/t23
http://www.demotest.net/categoey/c24
I only want "demotest" portion from above URL (using jquery and JavaScript )
Using window.location.hostname I got full path www.demotest.com but i want only demotest part
Upvotes: 1
Views: 1625
Reputation: 482
Split it by "." like so:
var str = "www.demotest.com"
var arrStr = str.split(".");
Then what you want will be at index 1 - arrStr[1]
Here is jsFiddle - http://jsfiddle.net/N0ir/yLtCW/
Upvotes: 0
Reputation: 6411
Try window.location.hostname.split('.')[0]
more at Extract hostname name from string
Upvotes: 0
Reputation: 72855
You could use this regular expression:
\.([^.]+)\.[^.]+$
Or you could split:
var domain = window.location.hostname.split('.');
console.log(domain[domain.length - 2]);
Upvotes: 2
Reputation: 148110
Try using split,
urlParts = window.location.hostname.split('.');
urlParts[urlParts.length-1]
Upvotes: 0