Home > database >  Tried to split string using regex. I cannot extract the parentheses
Tried to split string using regex. I cannot extract the parentheses

Time:01-29

I wanted my myResult = [ '40', '-', '(', '60', '*', '2', ')' ]

let myInput = '40-(60*2)';
let myResult = myInput.split(/([*\/ -])/);
console.log(myResult)

CodePudding user response:

Include parentheses in the character set, then use .match instead of .split and alternate with numeric characters.

let myInput = '40-(60*2)';
let myResult = myInput.match(/[()*\/ -]|\d /g);
console.log(myResult)

  •  Tags:  
  • Related