gpt4 book ai didi

javascript - 用于获取 url 路径名的正则表达式

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:52:30 25 4
gpt4 key购买 nike

我想提取完整路径,即路径后跟 JavaScript 中 URL 的主机名。

var url = 'https://domain.us/file.php/fghfgh?id=1'

这里我想提取/file.php/fghfgh?id=1

这怎么能只使用正则表达式而不是通过“document.createElement('a')”方法实现呢?

我需要正则表达式的答案,

最佳答案

如果您正在访问 location object , 你可以做

var path = location.href.replace(location.protocol+"//"+location.hostname,"")

var path = location.pathname+location.search

如果你有 # 你可能也需要添加它,正如 just_mad 所指出的:

var path = location.pathname+location.search+location.hash

关于javascript - 用于获取 url 路径名的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9680886/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com