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,191
  • 4
  • 17
  • 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
  • 68,675
  • 32
  • 163
  • 220
  • 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
  • 146,340
  • 25
  • 209
  • 204
0

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

more at Extract hostname name from string

Community
  • 1
  • 1
Aamir Afridi
  • 6,364
  • 3
  • 42
  • 42
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
  • 482
  • 7
  • 20