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

how to split this string and save different variable

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
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,910
  • 7
  • 38
  • 51
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
  • 11,171
  • 5
  • 29
  • 70
4b0
  • 21,981
  • 30
  • 95
  • 142