I'm trying to match between two characters, specifically the last part of a url but only between the last slash (/
) and a hash (#
).
我正在尝试匹配两个字符,特别是url的最后一部分,但只在最后一个斜杠(/)和一个散列(#)之间。
http://example.com/path/to/thing#name
The match should return
比赛应该返回
thing
I'm partly there now. I can either get the whole string of the last part of the url, or everything before hash (#
) but not both.
我现在部分。我可以获取url最后部分的整个字符串,也可以获取hash(#)之前的所有字符串,但不能同时获取。
/([^\/]*?.*(?=#))/
Please see my regex101 for testing.
请查看我的regex101进行测试。
1 个解决方案
#1
2
You are close but still overthinking it. This suffices:
你已经很接近了,但仍在思考。这就足够了:
/[^\/#]+(?=#|$)/
– a sequence of not-/
or #
characters, where the next one should be #
. You don't need to add parentheses to match it as a separate group, the match itself is correct. The final lookahead (?=#|$)
makes it stop on either an intervening #
or the end of the URL.
-非/或#字符序列,下一个字符应该是#。您不需要添加括号来将其作为单独的组匹配,匹配本身是正确的。最后的前视(?=#|$)使它在中间的#或URL的末尾停止。
See regex101.
看到regex101。
#1
2
You are close but still overthinking it. This suffices:
你已经很接近了,但仍在思考。这就足够了:
/[^\/#]+(?=#|$)/
– a sequence of not-/
or #
characters, where the next one should be #
. You don't need to add parentheses to match it as a separate group, the match itself is correct. The final lookahead (?=#|$)
makes it stop on either an intervening #
or the end of the URL.
-非/或#字符序列,下一个字符应该是#。您不需要添加括号来将其作为单独的组匹配,匹配本身是正确的。最后的前视(?=#|$)使它在中间的#或URL的末尾停止。
See regex101.
看到regex101。