1

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

Me7888
  • 1,111
  • 3
  • 15
  • 19

4 Answers4

2

You could use this regular expression:

\.([^.]+)\.[^.]+$

http://regex101.com/r/eM7oQ0

Or you could split:

var domain = window.location.hostname.split('.');
console.log(domain[domain.length - 2]);

http://jsfiddle.net/jyWYu/

brandonscript
  • 62,971
  • 29
  • 152
  • 211
  • its working on www.demotest.com but sometime url does not contain www portion........ so url only demotest.com – Me7888 Feb 25 '14 at 17:56
0

Try using split,

urlParts = window.location.hostname.split('.');
urlParts[urlParts.length-1]
Adil
  • 143,427
  • 25
  • 201
  • 198
0

Try window.location.hostname.split('.')[0]

more at Extract hostname name from string

Community
  • 1
  • 1
Aamir Afridi
  • 6,233
  • 3
  • 39
  • 41
0

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/

OutFall
  • 507
  • 6
  • 19