Javascript 如何在没有可选查询字符串参数的情况下从URL获取路径或slug
在以下URL示例中:Javascript 如何在没有可选查询字符串参数的情况下从URL获取路径或slug,javascript,regex,Javascript,Regex,在以下URL示例中: http://something.com/hello/world/this-is-a-slug http://something.com/hello/world/this-is-a-slug#awesome http://something.com/hello/world/this-is-a-slug?awesome=yes#world /hello/world/this-is-a-slug etc 我需要这是一个slug 以下是我到目前为止所做的工
http://something.com/hello/world/this-is-a-slug
http://something.com/hello/world/this-is-a-slug#awesome
http://something.com/hello/world/this-is-a-slug?awesome=yes#world
/hello/world/this-is-a-slug
etc
我需要这是一个slug
以下是我到目前为止所做的工作,它在存在查询字符串时起作用:
[^/]+?(.?(?=[\?]))
但是,当查询字符串不存在时,在正确执行时会遇到问题。我可以让它工作,但是用查询字符串打破它。这必须是正则表达式,没有其他方法。您可以使用表达式:
(?<=\/)[a-z-]+(?=[#a-z?=]+$|$)
(?我在这里面临的问题是,如果url在查询字符串或散列后有一个斜杠,它会提取错误的值(即/some/path?hello=world#/与此匹配)
)使用(?)?