Me7888
Me7888

Reputation: 1199

jquery - Get middle portion of URL path

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

Answers (4)

OutFall
OutFall

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

Aamir Afridi
Aamir Afridi

Reputation: 6411

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

more at Extract hostname name from string

Upvotes: 0

brandonscript
brandonscript

Reputation: 72855

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/

Upvotes: 2

Adil
Adil

Reputation: 148110

Try using split,

urlParts = window.location.hostname.split('.');
urlParts[urlParts.length-1]

Upvotes: 0

Related Questions