Skip to content
Advertisement

how to store the string “1+2+3” as an array like [“1″,”+”,”2″,”+”,”3″] in javascript?

var numbers = "3+3/2";

console.log(numbers);

var numArr = numbers.split(" ");
console.log(numArr);
numArr.splice(1, 3, '1');
console.log(numArr);
numbers = numArr.toString();

console.log(numbers);
var numbers = "3+3/2";

console.log(numbers);

var numArr = numbers.split(" ");
console.log(numArr);
numArr.splice(1, 3, '1');
console.log(numArr);
numbers = numArr.toString();

console.log(numbers);

I am trying to convert the whole string into an array. Then use the splice to edit the numArr Then change the original string, numbers

Advertisement

Answer

You could split the string with nonnumber characters.

var numbers = "3+3/2",
    parts = numbers.split(/(D+)/);

console.log(parts);
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement