Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - Get query string from a string?

Tags:

javascript

I'm trying to get the query string from a string (not the current URL).

For example, I have a URL 'www.google.com/?query=string', I'd like to be able to run a function on it and get '?query=string' back from it. How would I go about doing this?

Thanks

like image 635
Probocop Avatar asked Jan 19 '26 08:01

Probocop


2 Answers

Well, you can use a quick regexp that gets you the part you need:

myString.match(/(\?.*)/)[1]

Example:

'www.google.com/?query=string'.match(/(\?.*)/)[1] // evaluates to '?query=string'
like image 101
Alsciende Avatar answered Jan 20 '26 20:01

Alsciende


Window.location.search will evaluate to this.

http://www.w3schools.com/jsref/prop_loc_search.asp

like image 38
bryansh Avatar answered Jan 20 '26 20:01

bryansh