My goal is to extract specific values from a URL, focusing on the parameters and their respective values.
Since a parameter can appear multiple times in the URL, I need to retrieve all instances along with their corresponding values.
&myparameter[123]=1&myparameter[678]=4
The desired output should be ;123;1,;678;4
I attempted the following code, but I require the results to be comma-separated pairs rather than individual ones.
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 pair = ';' + x + ';' + c; console.log(pair); } } while (match);
How can I combine these results or suggest a more efficient method? Thank you.