Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 131185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:03:56+00:00 2026-05-11T06:03:56+00:00

I am trying to parse url-encoded strings that are made up of key=value pairs

  • 0

I am trying to parse url-encoded strings that are made up of key=value pairs separated by either & or &.

The following will only match the first occurrence, breaking apart the keys and values into separate result elements:

var result = mystring.match(/(?:&|&)?([^=]+)=([^&]+)/) 

The results for the string ‘1111342=Adam%20Franco&348572=Bob%20Jones’ would be:

['1111342', 'Adam%20Franco'] 

Using the global flag, ‘g’, will match all occurrences, but only return the fully matched sub-strings, not the separated keys and values:

var result = mystring.match(/(?:&|&)?([^=]+)=([^&]+)/g) 

The results for the string ‘1111342=Adam%20Franco&348572=Bob%20Jones’ would be:

['1111342=Adam%20Franco', '&348572=Bob%20Jones'] 

While I could split the string on & and break apart each key/value pair individually, is there any way using JavaScript’s regular expression support to match multiple occurrences of the pattern /(?:&|&)?([^=]+)=([^&]+)/ similar to PHP’s preg_match_all() function?

I’m aiming for some way to get results with the sub-matches separated like:

[['1111342', '348572'], ['Adam%20Franco', 'Bob%20Jones']] 

or

[['1111342', 'Adam%20Franco'], ['348572', 'Bob%20Jones']] 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:03:56+00:00Added an answer on May 11, 2026 at 6:03 am

    Hoisted from the comments

    2020 comment: rather than using regex, we now have URLSearchParams, which does all of this for us, so no custom code, let alone regex, are necessary anymore.

    – Mike ‘Pomax’ Kamermans

    Browser support is listed here https://caniuse.com/#feat=urlsearchparams


    I would suggest an alternative regex, using sub-groups to capture name and value of the parameters individually and re.exec():

    function getUrlParams(url) {   var re = /(?:\?|&(?:amp;)?)([^=&#]+)(?:=?([^&#]*))/g,       match, params = {},       decode = function (s) {return decodeURIComponent(s.replace(/\+/g, ' '));};    if (typeof url == 'undefined') url = document.location.href;    while (match = re.exec(url)) {     params[decode(match[1])] = decode(match[2]);   }   return params; }  var result = getUrlParams('http://maps.google.de/maps?f=q&source=s_q&hl=de&geocode=&q=Frankfurt+am+Main&sll=50.106047,8.679886&sspn=0.370369,0.833588&ie=UTF8&ll=50.116616,8.680573&spn=0.35972,0.833588&z=11&iwloc=addr'); 

    result is an object:

     {   f: 'q'   geocode: ''   hl: 'de'   ie: 'UTF8'   iwloc: 'addr'   ll: '50.116616,8.680573'   q: 'Frankfurt am Main'   sll: '50.106047,8.679886'   source: 's_q'   spn: '0.35972,0.833588'   sspn: '0.370369,0.833588'   z: '11' } 

    The regex breaks down as follows:

     (?:            # non-capturing group   \?|&         #   '?' or '&'   (?:amp;)?    #   (allow '&', for wrongly HTML-encoded URLs) )              # end non-capturing group (              # group 1   [^=&#]+      #   any character except '=', '&' or '#'; at least once )              # end group 1 - this will be the parameter's name (?:            # non-capturing group   =?           #   an '=', optional   (            #   group 2     [^&#]*     #     any character except '&' or '#'; any number of times   )            #   end group 2 - this will be the parameter's value )              # end non-capturing group 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I've URL encoded a word doc trying to parse certain fields..which is a
I'm trying to parse the url 'http://www.5min.com/handlers/SitemapHandler.ashx?type=videositemap&page=1' in python 2.7. The problem is when
I am trying to parse a FTP URL that has some special characters like
im trying to parse the XML returned by Google contacts API i made some
I'm trying to change instances of the following line: URL: http://www.google.com/?s= test to URL:
I'm trying to parse the following URI : http://translate.google.com/#zh-CN|en|你 but got this error message
I'm trying to pass an encrypted query string to another URL. The following code
I'm trying to parse a URI that has brackets - [ and ] -
I'm trying to parse a URL, but I'm having problems: http://www.meteor.gov.tr/deniz/deniz-suyu-sicakligi.aspx When I call
Hey guys, I am trying to create a script that will pull the log's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.