[英]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 ) 我只想要来自URL的“ demotest ”部分(使用jquery和JavaScript)
Using window.location.hostname I got full path www.demotest.com but i want only demotest part 使用window.location.hostname我有完整的路径www.demotest.com,但我只想要demotest部分
You could use this regular expression: 您可以使用以下正则表达式:
\.([^.]+)\.[^.]+$
http://regex101.com/r/eM7oQ0 http://regex101.com/r/eM7oQ0
Or you could split: 或者您可以拆分:
var domain = window.location.hostname.split('.');
console.log(domain[domain.length - 2]);
Try using split, 尝试使用拆分
urlParts = window.location.hostname.split('.');
urlParts[urlParts.length-1]
Try window.location.hostname.split('.')[0]
尝试
window.location.hostname.split('.')[0]
more at Extract hostname name from string 有关从字符串提取主机名的更多信息
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]
然后,您想要的就是索引1-
arrStr[1]
Here is jsFiddle - http://jsfiddle.net/N0ir/yLtCW/ 这是jsFiddle- http://jsfiddle.net/N0ir/yLtCW/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.