0

I want to get the anchor in a URL, where the anchor (#text) can be in these formats:

https://example.com/#section3?a=1a&b=2b
https://example.com/#section3&a=1a&b=2b
https://example.com/?a=1a&b=2b#section3
https://example.com/&a=1a&b=2b#section3

So I would have to get the following in all cases:

#section3

Anchors in the URL can only contain ASCII characters, so I though this could be used for that.

I got this, and although it seems to work, it seems to be it can be done better and shorter?

#(.)*\?|#(.)*?\&|#(.)*
Alvaro
  • 40,778
  • 30
  • 164
  • 336

1 Answers1

5

If I don't misunderstood your question then this is one of many solution.

#([^?&\s]+)

Demo: https://regex101.com/r/asDvr6/1

A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103