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

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -