How do I match a string with the the path in react

I have a react application where I wanted to render some menus depending upon the path.

I have following URL :

http://localhost:3000/Test/marks

Now from this URL I need to get the Test string only so that I can match that string with the array and then get the associated objects with that key.

I tried using the useLocation but it gives us the object and again I have to add a regex to get that name.

Is there any other way ?
Thanks