Skip to content
Advertisement

Get req params in client Javascript

Say I have a string like

JavaScript

This string matches the pattern

JavaScript

From the above string, how do I extract the userId and gameId values no matter their length(so not substring)

I know in ExpressJS you can do

JavaScript

But I need this to work in client side Javascript

Is there any way to do this?

Advertisement

Answer

The URL API is a safe and modern method that works server and client side:

location.pathname can be used if it is the url of the page you are on

link.pathname can be used for link objects

Say I have a string – then you need to make it a URL first

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement