John Resig's blog has a good example use-case of this. What’s the optimal way, in JavaScript, to convert a query string like “foo=1&foo=2&foo=3&blah=a&blah=b” into one that looks like this: “foo=1,2,3&blah=a,b”?
http://ejohn.org/blog/search-and-dont-replace/ On Oct 23, 5:15 am, pete otaqui <[email protected]> wrote: > Yes - I can see from both yours and Peter van der Zee's replies that "exec in > a loop" is a much better way of going. > > Thanks! > > Pete > > On 23 Oct 2011, at 12:52, Lasse Reichstein wrote: > > > > > > > > > On Sun, Oct 23, 2011 at 1:40 PM, pete otaqui <[email protected]> wrote: > > >> I wasn't really asking a question so much as noting down a useful > >> observation I'd made. > > >> As you can see from my examples, it's not just the matches from the global > >> RegExp that I'm after, it's also the sub patterns. These are never > >> returned by anything when using a global RegExp. > > > They are returned by RegExp.prototype.exec, and they are available if > > you use String.prototype.replace or String.prototype.split. > > If you need to access them, you can just to use .exec in a loop. > > > String methods like String.prototype.match are not the most basic > > operation on a RegExp, and it does indeed not return the captures when > > used with a global regexp. > > > Personally, I rarely, if ever, use String.prototype.match for anything. > > >> My message was just about a technique which actually uses .replace with a > >> callback rather than .match as a way of getting everything about each > >> capture (including lastIndex, and all the sub patterns). > > > That's another way to do it, but it's rather wasteful, since you build > > the result of .replace without actually needing it. > > /L > > > -- > > To view archived discussions from the original JSMentors Mailman list: > > http://www.mail-archive.com/[email protected]/ > > > To search via a non-Google archive, visit > > here:http://www.mail-archive.com/[email protected]/ > > > To unsubscribe from this group, send email to > > [email protected] -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/ To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/ To unsubscribe from this group, send email to [email protected]
