Reputation: 549
I'm trying to extract user IDs from a form serialized string, here is what I have so far.
Take
var dataString = "viewUsers_length=10&id%5B%5D=8163&id%5B%5D=8188&id%5B%5D=8141"
as the example string.
I split it into an array like this
var arrStr = dataString.split(/[=&]/);
which results in an array
[0]viewUsers_length
[1]10
[2]id%5B%5D
[3]8163
[4]id%5B%5D
[5]8188
[6]id%5B%5D
[7]8141
But I only want the ids (8163, 8188, 8141)
in reality this string could contain thousands of ids in this format. I've spent some time googling so far and haven't found anything that I think will work.
Upvotes: 0
Views: 85
Reputation: 4209
I would split on the "&" characters, then map a function to remove everything before the "=", and finally filter to remove elements that aren't id elements. My substring function is a bit crude, and could be improved (or replaced with regex) with more knowledge of the possible values.
dataString
.split("&")
.map(function(elt){return elt.startsWith("id") ? elt.substring(9,13) : ""})
.filter(function(elt){return elt != ""})
Upvotes: 0
Reputation: 151
Unfortunately because the parameter names for the ids are all the same, normal methods of parsing query strings might not be viable. So, this might be a job for regular expressions!
var dataString = "viewUsers_length=10&id%5B%5D=8163&id%5B%5D=8188&id%5B%5D=8141";
var regex = /&id%5B%5D=(\d+)/g;
var match;
var matches = [];
while (match = regex.exec(dataString)) {
matches.push(match[1]);
}
console.log(matches)
var list = matches.join(', ');
document.getElementById('output').innerHTML = list;
<div id="output"></div>
Upvotes: 1
Reputation: 2516
Try this
var dataString = "viewUsers_length=10&id%5B%5D=8163&id%5B%5D=8188&id%5B%5D=8141";
var myRegexp = /id%5B%5D=([0-9]+)/g;
match = myRegexp.exec(dataString);
while (match != null) {
console.log(match[1]);
match = myRegexp.exec(ddd);
}
outputs
8163
8188
8141
Upvotes: 0
Reputation: 26867
Filter the array.
There are native JavaScript ways using Array.prototype.filter()
var dataString = "viewUsers_length=10&id%5B%5D=8163&id%5B%5D=8188&id%5B%5D=8141"
var arrStr = dataString.split(/[=&]/);
var tester = /\d{4}/;
arrStr.filter(function (item) {
return tester.test(item);
});
/*
viewUsers_length
10
id%5B%5D
8163 -Match
id%5B%5D
8188 -Match
id%5B%5D
8141 -Match
*/
As well as jQuery ways using $().filter()
Upvotes: 0
Reputation: 5062
There are a couple of ways to address this. The first way, that I would use, would be to match the ID's with regex.
var ids = dataString.match(/id%5B%5D=[0-9]+/g);
Which would result in
["id%5B%5D=8163", "id%5B%5D=8188", "id%5B%5D=8141"]
So now we have the values, but we only want the numeric parts of it. So let's use map
to remove the rest
ids = ids.map(function (id) {
return id.replace('id%5B%5D=', '');
});
The other way would just be to filter on the array you already built, but you can see that you have the 10 in the second index, which would come back as a false positive, so I think this would provide a cleaner dataset
Upvotes: 0