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

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -