javascript: split a string into an array with different delimiters -
i have arithmetic string want parse result. best idea have come far convert string array, not sure correct (and cross compatible) way is.
the string have parse "-3-6-9-3+10-3-5-2" (d&d damage rolls , resistance initiative tracker). usable array, need split operator of + or -, not lose operator since need know operation perform.
is there way in javascript?
this array hope back: ["-3","-6","-9","-3","+10","-3","-5","-2"]
while using capturing parens valid approach, i'd recommend using lookahead split:
var str = '-3-6-9-3+10-3-5-2'; str.split(/(?=[-+])/); // no need check digits here // ["-3", "-6", "-9", "-3", "+10", "-3", "-5", "-2"]
in case, each split point placed before sign (i.e., +
or -
).
Comments
Post a Comment