加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

获取网址的某些部分(正则表达式)

发布时间:2020-12-14 00:36:06 所属栏目:百科 来源:网络整理
导读:给定URL(单行): http://test.example.com/dir/subdir/file.html 如何使用正则表达式提取以下部分: 子域(测试) 域(example.com) 没有文件的路径(/ dir / subdir /) 文件(file.html) 文件的路径(/dir/subdir/file.html) 没有路径的网址(http://test.example.
给定URL(单行):
http://test.example.com/dir/subdir/file.html

如何使用正则表达式提取以下部分:

>子域(测试)
>域(example.com)
>没有文件的路径(/ dir / subdir /)
>文件(file.html)
>文件的路径(/dir/subdir/file.html)
>没有路径的网址(http://test.example.com)
>(添加您认为有用的任何其他)

正则表达式应该正常工作,即使我输入以下URL:
http://example.example.com/example/example/example.html

谢谢。

A single regex to parse and breakup a
full URL including query parameters
and anchors e.g.

07000

^((http[s]?|ftp):/)?/?([^:/s]+)((/w+)*/)([w-.]+[^#?s]+)(.*)?(#[w-]+)?$

RexEx positions:

url: RegExp[‘$&’],

protocol:RegExp.$2,

host:RegExp.$3,

path:RegExp.$4,

file:RegExp.$6,

query:RegExp.$7,

hash:RegExp.$8

你可以进一步解析主机(‘。’分隔)很容易。

我会做的是使用这样的东西:

/*
    ^(.*:)//([A-Za-z0-9-.]+)(:[0-9]+)?(.*)$
*/
proto $1
host $2
port $3
the-rest $4

进一步解析“休息”尽可能具体。在一个正则表达式中做它是一个有点疯狂。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读