javascript - String split with RegExp returns empty strings at either end -
// pattern -- <some name optional 1/2/3 @ end separated _> var re = new regexp("^\(.*\)_\([1-3]\)$") "".split(re) // returns [""] --- ok "abc_d_4".split(re) // returns ["abc_d_4"] --- ok "abc_d_3".split(re) // returns ["", "abc_d", "3", ""] --- whoa! hoped-for ["abc_d", "3"] // similarly, "_3".split(re), "abc_d_3_4_3".split(re)
why empty strings @ either end in last case, , how avoid that? appreciate explanation.
i can see similar questions have been asked before on not case (or pl point me there)
moved comment (per request).
try split on /_(?=[1-3]$)/
Comments
Post a Comment