I have a string like this:
Cars: 5
Fruits: 2
Cars: 1
Carrier: 20
Cars: 20
Hey开发者_如何学Python: 2"
How do I get all the Cars values into an Array?
I want to end up with this:
[5, 1, 20]
Thanks.
I'd probably go with something like this, based on John Resig's "Search and don't replace" method.:
var arr = [];
str.replace(/Cars:\s(\d+)/g, function ($0, num) {
arr.push(+num);
});
Working demo: http://jsfiddle.net/wCLTe/1
精彩评论