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
Answer from Tomalak on Stack Overflow
Top answer
1 of 15
172

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
2 of 15
68

You need to use the 'g' switch for a global search

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

Regex (global mode): How to look for (and pass on) multiple occurrences of a string in a document? - Automation - DEVONtechnologies Community
Hello, is it possible using a regular expression in an smart rule to find multiple occurrences of a string in a document and then use this result to change the document’s name (by adding the multiple occurrences)? For example, the regular expression /hello/g looks for multiple occurrences ... More on discourse.devontechnologies.com
🌐 discourse.devontechnologies.com
0
July 10, 2022
Matching multiple occurrences in javascript using a regex - Stack Overflow
I am using Javascript and regex to parse some strings in "csv like flavour" with ; as separator. The regex I figured out so far is trying to get all the occurrences of a pattern like: "INTERESTING1 ( More on stackoverflow.com
🌐 stackoverflow.com
Javascript: How to get multiple matches in RegEx .exec results - Stack Overflow
When I run /(a)/g.exec('a a a ').length I get 2 but I thought it should return 3 because there are 3 as in the string, not 2! Why is that? I want to be able to search for all occurances of a ... More on stackoverflow.com
🌐 stackoverflow.com
Regex javascript to capture multiple occurrences - Stack Overflow
You have to use the g flag for this to work on multiple occurrences, and you need to match only words (\w), the dot (\.) and slash (\/) before the dot and the extension: let re = /([\w\.\/]*)\.(?:jpg|png)/g console.log( 'Hello ../aaa.jpg ../bbb.jpg ../sss.gif ../xxx.png End of Text'.match(re) ) ... They don't want the extensions. ... let regex ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript regex multiple matches
How to Match Multiple Occurrences With Regex in JavaScript | Delft Stack
February 2, 2024 - This tutorial discusses what a regular expression is and the different methods to match multiple occurrences in JavaScript.
🌐
TutorialsPoint
tutorialspoint.com › match-multiple-occurrences-in-a-string-with-javascript
Match multiple occurrences in a string with JavaScript?
To match multiple occurrences in a string, use regular expressions. Following is the code − · function checkMultipleOccurrences(sentence) { var matchExpression = /(JavaScript?[^\s]+)|(typescript?[^\s]+)/g; return sentence.match(matchExpression); } var sentence="This is my first JavaScript ...
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Regular_expressions
Regular expressions - JavaScript - MDN Web Docs
3 weeks ago - When you want to know whether a pattern is found in a string, use the test() or search() methods; for more information (but slower execution) use the exec() or match() methods. If you use exec() or match() and if the match succeeds, these methods return an array and update properties of the associated regular expression object and also of the predefined regular expression object, RegExp.
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-match-multiple-parts-of-a-string-using-regexp-in-javascript
JavaScript – How to Match Multiple Parts of a String Using RegExp? | GeeksforGeeks
December 6, 2024 - You can easily do this using global (g) and other RegExp features. The g flag enables global matching, which allows the regular expression to match all occurrences of a pattern in the string, not just the first one.
🌐
DEVONtechnologies Community
discourse.devontechnologies.com › devonthink › automation
Regex (global mode): How to look for (and pass on) multiple occurrences of a string in a document? - Automation - DEVONtechnologies Community
July 10, 2022 - Hello, is it possible using a regular expression in an smart rule to find multiple occurrences of a string in a document and then use this result to change the document’s name (by adding the multiple occurrences)? For example, the regular expression /hello/g looks for multiple occurrences of the string “hello” in a document (I basically use Regex101.com to check).
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › RegExp › exec
RegExp.prototype.exec() - JavaScript - MDN Web Docs
July 20, 2025 - If you are finding all occurrences of a global regex and you don't care about information like capturing groups, use String.prototype.match() instead. In addition, String.prototype.matchAll() helps to simplify matching multiple parts of a string (with capture groups) by allowing you to iterate ...
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Regular_expressions › Groups_and_backreferences
Groups and backreferences - JavaScript - MDN Web Docs
January 8, 2026 - // Backreferences const findDuplicates ... them. \w+ matches one or more word characters, and the parentheses () create a capturing group. The g flag is used to match all occurrences....
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › matchAll
String.prototype.matchAll() - JavaScript - MDN Web Docs
July 10, 2025 - The matchAll() method of String values returns an iterator of all results matching this string against a regular expression, including capturing groups. const regexp = /t(e)(st(\d?))/g; const str = "test1test2"; const array = [...str.matchAll(regexp)]; console.log(array[0]); // Expected output: ...
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › String › matchAll
JavaScript String matchAll() - Match All Occurrences | Vultr Docs
November 14, 2024 - Use matchAll() to retrieve all matches and convert the iterator to an array for easier manipulation. javascript Copy · const text = "The rain in Spain stays mainly in the plain."; const regex = /ain/g; const matches = Array.from(text.match...
🌐
The Valley of Code
thevalleyofcode.com › javascript-regular-expressions
How to use JavaScript Regular Expressions
Using the g flag is the only way to replace multiple occurrences in a string in vanilla JavaScript: "My dog is a good dog!".replace(/dog/g, 'cat') //My cat is a good cat! Groups let us do more fancy things, like moving around parts of a string: ...
🌐
Honeybadger
honeybadger.io › blog › javascript-regular-expressions
The ultimate JavaScript regex guide - Honeybadger Developer Blog
May 9, 2025 - Lazy quantifiers can be helpful when you want to match a pattern that occurs multiple times in a string, but you only want to match the first occurrence. For example, consider the following regular expression: const string = 'the cat sat on the mat'; const regex = /the.+?on/; const match = string.match(regex); console.log(match); // returns ['the cat sat on']
🌐
Stack Overflow
stackoverflow.com › questions › 33568613 › match-multiple-occurrences-regex-javascript
match multiple occurrences, regex javascript - Stack Overflow
November 6, 2015 - i want to find all occurrences of [anything-here] in sentence · I [am] John. [yesterday morning] я [русский] and Georgian [ქართული] and Indian [utf8-chars] and with symbols or space [good morning] or [hello, how are you?] the result must be [am] [yesterday morning] [русский] [ქართული] etc. ... ]Happy to hear you:re looking for those occurences. I'm pretty sure regexp can find them.