正则表达式从URL中删除主机名和端口? [英] Regular expression to remove hostname and port from URL?

查看:530
本文介绍了正则表达式从URL中删除主机名和端口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一些javascript来从url中删除hostname:port部分,这意味着我只想提取路径部分。

I need to write some javascript to strip the hostname:port part from a url, meaning I want to extract the path part only.

即。我想写一个函数getPath(url),这样getPath( http:// host:8081 / path / to / something )返回/ path / to / something

i.e. I want to write a function getPath(url) such that getPath("http://host:8081/path/to/something") returns "/path/to/something"

这可以使用正则表达式完成吗?

Can this be done using regular expressions?

推荐答案

快'n'脏:

^[^#]*?://.*?(/.*)$

在第一组中捕获主机名和端口(包括初始/)之后的所有内容。

Everything after the hostname and port (including the initial /) is captured in the first group.

这篇关于正则表达式从URL中删除主机名和端口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆