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

  • SEARCH
  • Home
  • 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 8414891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:17:14+00:00 2026-06-10T01:17:14+00:00

Examples first, questions second… Example 1) Non global match of ‘?sort=alpha&direction=asc’ ‘?sort=alpha&direction=asc’.match(/([^?&=]+)(=([^&]*))?/); Output: //

  • 0

Examples first, questions second…

Example 1) Non global match of ‘?sort=alpha&direction=asc’

'?sort=alpha&direction=asc'.match(/([^?&=]+)(=([^&]*))?/);

Output:

// ['sort=alpha', 'sort', '=alpha', 'alpha']

Example 2) Global match of ‘?sort=alpha&direction=asc’

'?sort=alpha&direction=asc'.match(/([^?&=]+)(=([^&]*))?/g);

Output:

// ['sort=alpha', 'sort', '=alpha', 'alpha']

Example 3) Global match replace of ‘?sort=alpha&direction=asc’

getRequestParameters: function () {
    var query_string = {},
        regex = new RegExp('([^?=&]+)(=([^&]*))?', 'g');

    '?sort=alpha&direction=asc'.replace(regex, function(match, p1, p2, p3, offset, string) {
        console.log(match, p1, p2, p3, offset, string);

        query_string[p1] = p3;
    });
}

Output:

// sort=alpha sort =alpha alpha 1 ?sort=alpha&direction=asc
// direction=asc direction =asc asc 12 ?sort=alpha&direction=asc 

My Questions

I am not sure if I could have ever figured this one out on my own, but I never “live” with a solution and I must figure out the rhyme behind the reason. The specific matches I think understand “fully enough”. I believe I know some of the answers below, but I rather not make assumptions and learn from smarter people!

  1. Why are 1) and 2) the same? (or are they?)
  2. What does the ‘sort=alpha’ mean in 1) and 2)?
  3. Why does 2) not return both sort and direction parameters?
  4. What is the 3) iterating over with the .replace()?
  5. Is there a way of capturing N parameters without .replace()?

Thanks!

update

var regex = new RegExp('([^?&=]+)(=([^&]*))?');
regex.exec('?sort=alpha&direction=asc');
// Chrome 21 - ["sort=alpha", "sort", "=alpha", "alpha"]

var regex = new RegExp('([^?&=]+)(=([^&]*))?', 'g');
regex.exec('?sort=alpha&direction=asc');
// Chrome 21 - ["sort=alpha", "sort", "=alpha", "alpha"]

'?sort=alpha&direction=asc'.match(/([^?&=]+)(=([^&]*))?/);
// Chrome 21 - ["sort=alpha", "sort", "=alpha", "alpha"]

'?sort=alpha&direction=asc'.match(/([^?&=]+)(=([^&]*))?/g);
// Chrome 21 - ["sort=alpha", "direction=asc"]

var regex = new RegExp('([^?&=]+)(=([^&]*))?', 'g');
regex.lastIndex = 11;
regex.exec('?sort=alpha&direction=asc');
// Chrome 21 - ["direction=asc", "direction", "=asc", "asc"]

In summary, Example 3) is still correct, but go to this answer for a more qualified response.

end update

References and thanks to Steven Benner:

  • https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/match
  • https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/replace
  • http://stevenbenner.com/2010/03/javascript-regex-trick-parse-a-query-string-into-an-object/
  • http://www.bloggingdeveloper.com/post/JavaScript-QueryString-ParseGet-QueryString-with-Client-Side-JavaScript.aspx (similar topic so I threw it in)
  • 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. Editorial Team
    Editorial Team
    2026-06-10T01:17:15+00:00Added an answer on June 10, 2026 at 1:17 am

    Answers first, question afterwards:

    1. In both Chrome 21 and Firefox 14 I get ["sort=alpha", "direction=asc"] for the g version – so they are not the same at all.
    2. The first returned value from match is the entire matched string (in this case one or more characters that are not an ampersand, question mark or equal sign optionally followed by an equal sign and zero or more characters that are not an ampersand).
    3. It does (see answer to #1) – what browser are you running these tests in?
    4. replace is iterating over each match it finds in the string.
    5. Use multiple calls to exec or use the existing regex you have:

      > '?sort=alpha&direction=asc&another=value'.match(/([^?&=]+)(=([^&]*))?/g);
      ["sort=alpha", "direction=asc", "another=value"]
      

    What browser are you using that gave you the results you provided for your first questions?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two examples on jsFiddle: http://jsfiddle.net/9uhfX/1/ http://jsfiddle.net/9uhfX/2/ Example 1 In the first one
I am implementing my first actual non-tutorial Backbone app, and have 2-ish questions about
This is just a simple question. Either way works. I prefer my first example,
Please see following examples first, and which one is better? Could you compare some
In the following examples: the first seems more verbose but less wasteful of resources
I'm writing my first iphone app and through several examples I've got a UITableView
Scheduled the first java Quartz job successfully with the instructions mentioned in - http://quartz-scheduler.org/documentation/quartz-2.1.x/examples/Example1
I'm using opengl and trying to create a first person camera. All examples use
I am following this example (First comment), How to create a custom ListView with
Using the Programming F# book, there's an example first given as serial: let result1

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.