2

I'm trying extract expecific values from a URL. The portions to be extracted are part of the parameter and its respective value.

Because the parameter may be present multiple times I would need to grab all instances along with their respective value

&myparameter[123]=1&myparameter[678]=4

Output should be ;123;1,;678;4

I tried the below but I need them as comma separated, not as individual pairs

var regex1 = /&myparameter\[(.*?)\]=/g;
var regex2 = /\]=(.*?)\&/g;
var input = '&myparameter[123]=1&myparameter[678]=4&otherthings=rtz';
var match;
var y = [];
do {
  match = regex1.exec(input);
  match2 = regex2.exec(input);
  if (match) {
    var x = match[1];
    var c = match2[1];
    var y = ';' + x + ';' + c;
    console.log(y);
  }
} while (match);

How would I join these results or point me to a more effective way of doing so. Thank you.

nem035
  • 34,790
  • 6
  • 87
  • 99
Mike-S122
  • 189
  • 3
  • 14
  • Do you need them extracted into a comma separated string or as an array inside an object? (I would expect the latter instead of the former) – wizebin Jul 20 '17 at 17:30
  • @nem035: Yes, array would be good way. Important is that the final print of those values is in the mentioned format. – Mike-S122 Jul 20 '17 at 17:35
  • @wizebin I would have expected the latter however if the params are repeated, that probably wouldn't work? There doesn't seem to be a definitive standard on whether you can reuse params in a url - https://stackoverflow.com/questions/11621477/using-duplicate-parameters-in-a-url – sauntimo Jul 20 '17 at 17:35

4 Answers4

0

Within the loop you should just add your values into an array and then print them (or use them as needed) outside the loop.

The code bellow assumes you need to build an array of pairs that you can latter join with commas if needed (there's also the commented part that builds a string directly if that is your desired approach).

Here's how you can do this with minimum changes to your code:

var regex1 = /&myparameter\[(.*?)\]=/g;
var regex2 = /\]=(.*?)\&/g;
var input = '&myparameter[123]=1&myparameter[678]=4&otherthings=rtz';
var match;
var y = [];
// var y = ''; // if building a string instead
do {
  match = regex1.exec(input);
  match2 = regex2.exec(input);
  if (match) {
    var x = match[1];
    var c = match2[1];
    y.push(';' + x + ';' + c);
    // or, if `y` is a string, `y += ';' + x + ';' + c;
  }
} while (match);

console.log(y.join(','));
// console.log(y); if building a string
nem035
  • 34,790
  • 6
  • 87
  • 99
  • Thank very much. The array approach seems indeed the best as it allows me to use the array for other purposes too (apart from the mentioned output). Much appreciated – Mike-S122 Jul 20 '17 at 17:42
  • @Mike-S122 no problem mate, glad to help :) – nem035 Jul 20 '17 at 17:44
0

I wrote a Gist to get URL params as a javascript object in one line of javascript. I don't think it will cope with the same params used more than once, that seems strange to me. But this might help.

console.log( JSON.parse('{"' + decodeURI(window.location.href.replace(/^(.*\?)/, '').replace(/&/g, "\",\"").replace(/=/g,"\":\"")) + '"}') );
sauntimo
  • 1,531
  • 1
  • 17
  • 28
0

You only need a single regex with capture groups. On each iteration push the string into an array, and in the end join them:

var regex = /&myparameter\[(.*?)\]=(\d+)/g;
var input = '&myparameter[123]=1&myparameter[678]=4&otherthings=rtz';
var y = [];
var match;
while ((match = regex.exec(input)) !== null) {
  y.push(';' + match[1] + ';' + match[2]);
}

var result = y.join(',');

console.log(result);
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
0

var regex= new RegExp(/&myparameter\[([0-9]+)\]=([0-9]+)/,'g');
var str= '&myparameter[123]=1&myparameter[678]=4&otherthings=rtz';

var match;
var res='';
while ((match= regex.exec(str)) !== null) {  
    res+=';'+match[1]+';'+match[2]+',';
}
console.log(res);
C.Vergnaud
  • 857
  • 6
  • 15