-10
"2+3-8*5"
 2
 3
 8
 5

how to split this string and save different variable

Abdulla Nilam
  • 31,770
  • 15
  • 58
  • 79
Eternal09
  • 21
  • 4

2 Answers2

0

Here you can split your string into an array for multiple delimiters.

var strVal = "2+3-8*5";

var resp = strVal.split(/(?:\+|\-|\*)/);

console.log("Resp: " , resp);
Gufran Hasan
  • 8,059
  • 7
  • 33
  • 48
0

@Gufran solution is great with regex. If you don't want to use regex you can use isNaN with loop.

var str = "2+3-8*5";
var result = [];
for (var i = 0; i < str.length; i++) {
  if (!isNaN(parseInt(str.charAt(i), 10))) {
    result.push(str.charAt(i));
  }
}
console.log(result);
Adam Azad
  • 10,915
  • 5
  • 26
  • 67
4b0
  • 20,627
  • 30
  • 92
  • 137