Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85653

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

Upvotes: 1

Views: 54

Answers (2)

JayRow
JayRow

Reputation: 139

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);

Upvotes: 0

James Lin
James Lin

Reputation: 26598

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]];

Upvotes: 1

Related Questions