Reputation: 615
Getting Numbers from a string and inserting them into array. splitting the string would result into single character and thus does not solve my problem.
var str="(235+456+2+3-6-(2*5))"
Output Must be:
[235,456,2,3,6,2,5]
Upvotes: 1
Views: 77
Reputation: 31
If we are talking about number output:
"(235+456+2+3-6-(2*5))".match(/\d+/g).map(e=>+e);
Upvotes: 0
Reputation: 92894
The solution using String.match
function:
var str="(235+456+2+3-6-(2*5))"
numbers = str.match(/\b\d+?\b/g);
console.log(numbers); // ["235", "456", "2", "3", "6", "2", "5"]
Upvotes: 1
Reputation: 386868
You could use a regular expression for it. It looks only for connected numbers.
console.log('(235+456+2+3-6-(2*5))'.match(/\d+/g));
Upvotes: 2