如何将字符串 "1+2+3" 存储为数组,如 ["1","+","2","+","3"] in javascript?
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
我会使用正则表达式来匹配数字或非space、非数字字符:
var numbers = "3+3/2";
console.log(
numbers.match(/\d+|[^\s\d]+/g)
);
您可以使用非数字字符拆分字符串。
var numbers = "3+3/2",
parts = numbers.split(/(\D+)/);
console.log(parts);
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
我会使用正则表达式来匹配数字或非space、非数字字符:
var numbers = "3+3/2";
console.log(
numbers.match(/\d+|[^\s\d]+/g)
);
您可以使用非数字字符拆分字符串。
var numbers = "3+3/2",
parts = numbers.split(/(\D+)/);
console.log(parts);