简体   繁体   中英

how to take numbers from string and store in an array?

var str = 'asdf234awe324wese34'; // or string -> '23asdre34ere' or something

numbers should be in an array like this:

var strArr = [234,324,34]; // if string or -> [23,34] or something

might be answer?

var a = '23wer234sdf45';
var reg = /\d+/g;
var store = a.match(reg);
//alert(store[2]);//alerts 45

But now how can I store it in an array?

var storeArray = []; //insert matched numbers here

So that I could use it by calling apply() method later in my project....

no such project, just to learning for fun

var a = '23wer234sdf45';
var reg = /\d+/g;
var store = a.match(reg);
console.log(store[0]);
console.log(store[1]);
console.log(store[2]);

Like everyone else said it's already an array, but in case not then:

var storeArray = [store[0], store[1], store[2]];

You can use the substring method .

int str = 'asdf234awe324wese34';
var storeArray = [];
storeArray[0] = str.subString(4,7);
storeArray[1] = str.subString(10,13);
storeArray[2] = str.subString(17,19);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM