使用 JavaScript 或 jQuery 解析 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7840306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
parse URL with JavaScript or jQuery
提问by chris
Ok lets say I have a URL
好吧,假设我有一个 URL
example.com/hello/world/20111020 (with or without the trailing slash). What I would like to do is strip from the url the domain example.com. and then break the hello world 20111020 into an array. But my other problem is. Sometimes the URL has no /hello/world/20111020 or just /hello/ so I need to first determine if there is anything after example.com if there not, then do nothing as obviously there's nothing to work with. However if there is something there for each / I need to add it to this array in order. So I can work with the array[0] and know it was hello.
example.com/hello/world/20111020(带或不带斜杠)。我想做的是从 url 中删除域 example.com。然后将 hello world 20111020 分解成一个数组。但我的另一个问题是。有时 URL 没有 /hello/world/20111020 或只有 /hello/ 所以我需要首先确定在 example.com 之后是否有任何东西,如果没有,然后什么都不做,因为显然没有什么可使用的。但是,如果每个/我需要按顺序将它添加到这个数组中。所以我可以使用数组 [0] 并知道它是你好。
I tried something a couple days back but was running into issues with trailing slashes it kept breaking the script, I unfortunately abandoned that idea. And today I am looking for fresh ideas.
几天前我尝试了一些东西,但遇到了尾部斜杠的问题,它不断破坏脚本,不幸的是我放弃了这个想法。今天我正在寻找新的想法。
回答by Narendra Yadala
This should work
这应该工作
var url = 'example.com/hello/world/20111020/';
//get rid of the trailing / before doing a simple split on /
var url_parts = url.replace(/\/\s*$/,'').split('/');
//since we do not need example.com
url_parts.shift();
Now url_parts
will point to the array ["hello", "world", "20111020"]
.
现在url_parts
将指向数组["hello", "world", "20111020"]
。
回答by James Johnson
You can use the jQuery-URL-Parserplugin:
您可以使用jQuery-URL-Parser插件:
var file = $.url.attr("file");
In your case you'd probably want to use segment()
:
在您的情况下,您可能想要使用segment()
:
var segments = $.url('http://allmarkedup.com/folder/dir/example/index.html').segment();
// segments = ['folder','dir','example','index.html']
回答by DefyGravity
<script type="text/javascript">
function splitThePath(incomingUrl){
var url = document.createElement("a");
url.href = incomingUrl;
//url.hash Returns the anchor portion of a URL
//url.host Returns the hostname and port of a URL
//url.hostname Returns the hostname of a URL
//url.href Returns the entire URL
//url.pathname Returns the path name of a URL
//url.port Returns the port number the server uses for a URL
//url.protocol Returns the protocol of a URL
//url.search Returns the query portion of a URL
if(url.pathname && url.pathname != ""){
var pathnameArray = url.pathname.split("/");
}else{
}
}
</script>
回答by Ed Heal
I have created the following regular expression for URLs
我为 URL 创建了以下正则表达式
^https?://(((0|([1-9][0-9]{0,1}))(\.(0|([1-9][0-9]{0,1}))){3})|([a-zA-Z]([a-zA-Z0-9$\-_@\.&+!*"\'\(\),]|(%[0-9a-fA-F][0-9a-fA-F]))*(\.([a-zA-Z]([a-zA-Z0-9$\-_@\.&+!*"\'\(\),]|(%[0-9a-fA-F][0-9a-fA-F]))*))*))(/|((/([a-zA-Z]([a-zA-Z0-9$\-_@\.&+!*"\'\(\),]|(%[0-9a-fA-F][0-9a-fA-F]))*))*))$
It has been written for MySql - I am sure with a bit of fiddling you can get it you work for your needs.
它是为 MySql 编写的——我相信只要稍微摆弄一下,你就可以让它满足你的需要。
BTW - I took the idea from an RFC - The number escapes me at this moment
顺便说一句 - 我从一个 RFC 中得到了这个想法 - 这个数字此刻让我不知所措
回答by Shoaib Nawaz
For parsing URLs, one different approach can be using anchor DOM object.
对于解析 URL,一种不同的方法是使用锚 DOM 对象。
var a = document.createElement("A");
a.href = 'http://example.com:8080/path/to/resources?param1=val1¶ms2=val2#named-anchor';
a.protocol; // http:
a.host; // example.com:8080
a.hostname; //example.com
a.port; // 8080 (in case of port 80 empty string returns)
a.pathname; // /path/to/resources
a.hash; // #named-anchor
a.search // ?param1=val1¶ms2=val2