How can I match multiple occurrences with a regex in JavaScript similar to PHP's preg_match_all()?

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

(^=#+)=?(^&#*)/g, match, params = {}, decode = function (s) {return decodeURIComponent(s. Replace(/\+/g, " "));}; while (match = re. Exec(url)) { paramsdecode(match1) = decode(match2); } 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" }.

3 This is what I was hoping for. What I've never seen in JavaScript documentation is mention that the exec() method will continue to return the next result set if called more than once. Thanks again for the great tip!

– Adam Franco Feb 6 '09 at 16:44 1 It does because of this: regular-expressions. Info/javascript. Html (Read through: "How to Use The JavaScript RegExp Object") – Tomalak Feb 6 '09 at 16:54 1 there is a bug in this code: the semicolon after the "while" should be removed.

– Jan Willem B Feb 23 '10 at 16:31 @Jan: Sharp eyes, you are right! Fixed, thanks for the hint. – Tomalak Feb 23 '10 at 19:55.

You need to use the 'g' switch for a global search var result = mystring. Match(/(&|&)?(^=+)=(^&+)/g).

Set the g modifier for a global match: /…/g.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions