Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在没有可选查询字符串参数的情况下从URL获取路径或slug_Javascript_Regex - Fatal编程技术网

Javascript 如何在没有可选查询字符串参数的情况下从URL获取路径或slug

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 以下是我到目前为止所做的工

在以下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

以下是我到目前为止所做的工作,它在存在查询字符串时起作用:

[^/]+?(.?(?=[\?]))

但是,当查询字符串不存在时,在正确执行时会遇到问题。我可以让它工作,但是用查询字符串打破它。这必须是正则表达式,没有其他方法。

您可以使用表达式:

(?<=\/)[a-z-]+(?=[#a-z?=]+$|$)

(?我在这里面临的问题是,如果url在查询字符串或散列后有一个斜杠,它会提取错误的值(即
/some/path?hello=world#/与此匹配)
)使用
(?)?