使用 JavaScript 或 jQuery 解析 URL

发布于 2024-12-11 05:17:41 字数 376 浏览 3 评论 0原文

好吧,假设我有一个 URL

example.com/hello/world/20111020 (带或不带尾部斜杠)。 我想要做的是从 url 中删除域名 example.com。然后将 hello world 20111020 分解成一个数组。但我的另一个问题是。有时,URL 没有 /hello/world/20111020 或只有 /hello/,所以我需要首先确定 example.com 之后是否有任何内容,如果没有,则不执行任何操作,因为显然没有任何内容可处理。但是,如果每个 / 都有一些东西,我需要将其按顺序添加到该数组中。所以我可以使用 array[0] 并知道这是你好。

几天前我尝试了一些东西,但遇到了尾随斜杠的问题,它不断破坏脚本,不幸的是我放弃了这个想法。今天我正在寻找新的想法。

Ok lets say I have a 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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

倚栏听风 2024-12-18 05:17:41

这应该可以工作

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(); 

现在 url_parts 将指向数组 ["hello", "world", "20111020"]

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"].

瑾兮 2024-12-18 05:17:41

您可以使用 jQuery-URL-Parser 插件:

var file = $.url.attr("file"); 

在您的情况下,您可能想要使用segment()

var segments = $.url('http://allmarkedup.com/folder/dir/example/index.html').segment(); 

// segments = ['folder','dir','example','index.html']

You can use the jQuery-URL-Parser plugin:

var file = $.url.attr("file"); 

In your case you'd probably want to use segment():

var segments = $.url('http://allmarkedup.com/folder/dir/example/index.html').segment(); 

// segments = ['folder','dir','example','index.html']
一个人的旅程 2024-12-18 05:17:41
   <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>
   <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>
若水般的淡然安静女子 2024-12-18 05:17:41

我已经为 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]))*))*))$

它是为 MySql 编写的 - 我相信只要稍微摆弄一下,您就可以让它满足您的需求。

顺便说一句 - 我从 RFC 中得到了这个想法 - 现在我忘记了这个数字

I have created the following regular expression for URLs

^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.

BTW - I took the idea from an RFC - The number escapes me at this moment

时光倒影 2024-12-18 05:17:41

对于解析 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

For parsing URLs, one different approach can be using anchor DOM object.

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
千柳 2024-12-18 05:17:41

您可以使用 JS 的 URL 构造函数,并且通过将 URL 的 pathname 值按 / 拆分来检索 pathnames 数组

const urlParts = (url) => {
  if (!/^https?:\/\//.test(url)) url = `https://${url}`;
  const data = new URL(url);
  data.pathnames = data.pathname.split("/").filter(Boolean);
  return data;
};

console.log(urlParts("example.com/hello/world/20111020").pathnames);
console.log(urlParts("example.com/hello/world/20111020/").pathnames);
console.log(urlParts("example.com/hello/world/20111020?xyz=123").pathnames);
console.log(urlParts("example.com/hello/").pathnames);
console.log(urlParts("example.com").pathnames);

You could make use of JS's URL constructor, and retrieve the pathnames array by splitting the URL's pathname value by /

const urlParts = (url) => {
  if (!/^https?:\/\//.test(url)) url = `https://${url}`;
  const data = new URL(url);
  data.pathnames = data.pathname.split("/").filter(Boolean);
  return data;
};

console.log(urlParts("example.com/hello/world/20111020").pathnames);
console.log(urlParts("example.com/hello/world/20111020/").pathnames);
console.log(urlParts("example.com/hello/world/20111020?xyz=123").pathnames);
console.log(urlParts("example.com/hello/").pathnames);
console.log(urlParts("example.com").pathnames);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文