I am trying to parse url-encoded strings that are made up of key=value pairs separated by either &
or &
.
The following will only match the first occurrence, breaking apart the keys and values into separate result elements:
var result = mystring.match(/(?:&|&)?([^=]+)=([^&]+)/)
The results for the string '1111342=Adam%20Franco&348572=Bob%20Jones' would be:
['1111342', 'Adam%20Franco']
Using the global flag, 'g', will match all occurrences, but only return the fully matched sub-strings, not the separated keys and values:
var result = mystring.match(/(?:&|&)?([^=]+)=([^&]+)/g)
The results for the string '1111342=Adam%20Franco&348572=Bob%20Jones' would be:
['1111342=Adam%20Franco', '&348572=Bob%20Jones']
While I could split the string on &
and break apart each key/value pair individually, is there any way using JavaScript's regular expression support to match multiple occurrences of the pattern /(?:&|&)?([^=]+)=([^&]+)/
similar to PHP's preg_match_all()
function?
I'm aiming for some way to get results with the sub-matches separated like:
[['1111342', '348572'], ['Adam%20Franco', 'Bob%20Jones']]
or
[['1111342', 'Adam%20Franco'], ['348572', 'Bob%20Jones']]
Source: https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec
Finding successive matches
If your regular expression uses the "g" flag, you can use the exec() method multiple times to find successive matches in the same string. When you do so, the search starts at the substring of str specified by the regular expression's lastIndex property (test() will also advance the lastIndex property). For example, assume you have this script:
This script displays the following text:
Note: Do not place the regular expression literal (or RegExp constructor) within the while condition or it will create an infinite loop if there is a match due to the lastIndex property being reset upon each iteration. Also be sure that the global flag is set or a loop will occur here also.
Just to stick with the proposed question as indicated by the title, you can actually iterate over each match in a string using
String.prototype.replace()
. For example the following does just that to get an array of all words based on a regular expression:If I wanted to get capture groups or even the index of each match I could do that too. The following shows how each match is returned with the entire match, the 1st capture group and the index:
After running the above,
words
will be as follows:In order to match multiple occurrences similar to what is available in PHP with
preg_match_all
you can use this type of thinking to make your own or use something likeYourJS.matchAll()
. YourJS more or less defines this function as follows: