给定 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.
https://www.google.com/dir/1/2/search.html?arg=0-a&arg1=1-b&arg3-c#hash
^((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
进一步解析“其余部分”,使其尽可能具体。在一个正则表达式中执行此操作有点疯狂。
关于regex - 获取 URL 的一部分(正则表达式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27745/