[英]javascript iterate over string
I have a string like this: 我有一个像这样的字符串 :
Cars: 5
Fruits: 2
Cars: 1
Carrier: 20
Cars: 20
Hey: 2"
How do I get all the Cars values into an Array? 如何将所有Cars值放入数组?
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.: 我可能会基于John Resig的“搜索且不替换”方法使用类似的方法:
var arr = [];
str.replace(/Cars:\s(\d+)/g, function ($0, num) {
arr.push(+num);
});
Working demo: http://jsfiddle.net/wCLTe/1
工作演示: http : //jsfiddle.net/wCLTe/1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.