Javascript - Get query string from a string? - Stack Overflow

I'm trying to get the query string from a string (not the current URL).For example, I have a URL &

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

For example, I have a URL 'www.google/?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

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

For example, I have a URL 'www.google./?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

Share Improve this question asked Mar 16, 2010 at 14:52 ProbocopProbocop 10.5k28 gold badges89 silver badges117 bronze badges 2
  • Do you need to parse the query string also? Eg to get an object with obj.query == 'string' back? – Roatin Marth Commented Mar 16, 2010 at 14:56
  • Hi, No i'm just after the full query string, it's for an SEO reporting tool I'm working on. I think Alsciende's answer is what I am after. – Probocop Commented Mar 16, 2010 at 14:57
Add a ment  | 

4 Answers 4

Reset to default 6

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

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

Example:

'www.google./?query=string'.match(/(\?.*)/)[1] // evaluates to '?query=string'

Window.location.search will evaluate to this.

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

There's a jQuery plugin for that.

If you're using jQuery, use this plugin: http://projects.allmarkedup./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./?query=string").attr("query") // returns 'query=string'

Or also get a specific parameter:

$.url.setUrl("www.google./?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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744728918a4590347.html

相关推荐

  • Javascript - Get query string from a string? - Stack Overflow

    I'm trying to get the query string from a string (not the current URL).For example, I have a URL &

    12小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信