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 OverflowHoisted 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
You need to use the 'g' switch for a global search
var result = mystring.match(/(&|&)?([^=]+)=([^&]+)/g)
See this question:
txt = "Local residents o1__have called g__in o22__with reports...";
var regex = /o([0-9]+)__/g
var matches = [];
var match = regex.exec(txt);
while (match != null) {
matches.push(match[1]);
match = regex.exec(txt);
}
alert(matches);
You need to use .exec() on a regular expression object and call it repeatedly with the g flag to get successive matches like this:
var txt = "Local residents o1__have called g__in o22__with reports...";
var re = /o([0-9]+)__/g;
var matches;
while ((matches = re.exec(txt)) != null) {
alert(matches[1]);
}
The state from the previous match is stored in the regular expression object as the lastIndex and that's what the next match uses as a starting point.
You can see it work here: http://jsfiddle.net/jfriend00/UtF6J/
Using the regexp this way is described here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp/exec.
Regex (global mode): How to look for (and pass on) multiple occurrences of a string in a document? - Automation - DEVONtechnologies Community
Matching multiple occurrences in javascript using a regex - Stack Overflow
Javascript: How to get multiple matches in RegEx .exec results - Stack Overflow
Regex javascript to capture multiple occurrences - Stack Overflow
exec() is returning only the set of captures for the first match, not the set of matches as you expect. So what you're really seeing is $0 (the entire match, "a") and $1 (the first capture)--i.e. an array of length 2. exec() meanwhile is designed so that you can call it again to get the captures for the next match. From MDN:
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).
You could use match instead:
'a a a'.match(/(a)/g).length // outputs: 3
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)
)
let regex = /\S+?(?=\.(?:jpg|png))/g
console.log(
'Hello ../aaa.jpg blah ../a-b-c.jpg ../sss.gif ../&%01-x.png End of Text'.match(regex)
)
Where
\S+?matches 1 or more (not greedy) non space character(?= ... )is a positive lookahead(?: ... )is a non capture group