Reputation: 3318
I've got a bunch of strings that look like this:
Using javascript, how can I get an array of 10,4,77? These will be values of input fields all with the class form-text so I have the option of iterating of them.
Thanks, Howie
Upvotes: 1
Views: 51
Reputation: 1038
Lets say you have the given strings in array. Then using Array.map
you can retrieve the id's
var a = ["This is string 1 [id:10]", "This is string 2 [id:4]", "This is string 3 [id:77]"];
var result = a.map(function (item) {
return /.*?\[id:([\d]{1,})\]/g.exec(item)[1];
});
console.log(result);
Upvotes: 2
Reputation: 13222
You could do something like this:
var test = "[id:10]";
var result = test.split(":")[1].split("]")[0];
//result should have 10
Upvotes: 1