Reputation: 10552
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
Upvotes: 2
Views: 2074
Reputation: 303
Window.location.search will evaluate to this.
http://www.w3schools.com/jsref/prop_loc_search.asp
Upvotes: 2
Reputation: 14031
If you're using jQuery, use this plugin: http://projects.allmarkedup.com/jquery_url_parser/ This one lets you operate on the document's url, or any URL string
Then you can do:
$.url.setUrl("www.google.com/?query=string").attr("query") // returns 'query=string'
Or also get a specific parameter:
$.url.setUrl("www.google.com/?query=string").param("query") // returns 'string'
But if you really just need the whole query string, a quick regex like Alsciende suggested is the way to go.
Upvotes: 0
Reputation: 26971
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'
Upvotes: 6