I\'ve the following string splitting JavaScript code:
var formula = \"(field1 + field2) * (field5 % field2) / field3\";
console.log(formula.split(/[+(-)% *\\/]/)
Instead of splitting on /[+(-)% *\/]/
split on more than one: /[+(-)% *\/]+/
. You still might get empty matches at the start and end. To solve that problem you can use a similar regex with replace:
formula.replace(/^[+(-)% *\/]+|[+(-)% *\/]+$/g, "").split(/[+(-)% *\/]+/)
So
var formula = "(field1 + field2) * (field5 % field2) / field3";
console.log(formula.replace(/^[+(-)% *\/]+|[+(-)% *\/]+$/g, "").split(/[+(-)% *\/]+/));
yields
field1,field2,field5,field2,field3