1

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:

Community
  • 1
  • 1
chemoish
  • 1,210
  • 2
  • 13
  • 23

1 Answers1

3

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?

Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
  • Store a regex literal in a var then you can call `while (reg.exec(str))` for iteration as well. The behavior which you presented is the very same described in OP's [documentation](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/match). – Fabrício Matté Aug 23 '12 at 03:15
  • @SeanVieira 1) I am getting your results at home in Chrome 21 and Firefox 12. 2) Wanted to make sure. 3) I will find out browser versions when I go to work (Originally posted in Google Chrome). 4) Makes much more sense (I don't think I would have asked this question otherwise) in the context of ['sort=alpha', 'direction=asc'] // summaries of the full result set (still kind of confusing, would rather have to fully captured results) 5) See number 4. – chemoish Aug 23 '12 at 04:33
  • @FabrícioMatté Not following the while statement nor your reference to OP's documentation. Do you mean that it will iterate through a loop twice? – chemoish Aug 23 '12 at 04:38
  • @SeanVieira might be a browser regex quick - either showing the results summary or the full match of the first match only... will have to do some more research! – chemoish Aug 23 '12 at 04:40
  • @chemoish My comment was based on your 5th question (alternative for replace). Check [this answer](http://stackoverflow.com/a/10876450/1331430) for more detail. – Fabrício Matté Aug 23 '12 at 04:42
  • @chemoish - `match` is supposed to behave that way - it follows the same rules as `exec` (When the `g` flag is not provided it gives you every *capture group*; when it is provided it simply gives you every match). – Sean Vieira Aug 23 '12 at 04:42
  • @FabrícioMatté Thanks, the answer provided in your comment summarized almost everything. I wish the comments section was better so I'll add my final conclusions to my original post. – chemoish Aug 23 '12 at 04:54