javascript - JS string split using regex confusion -
hi trying split string using regex results different expect.
var text = 'p$blank$a$blank$l$blank$a$blank$c$blank$e$blank$'; > text.split(/(\$blank\$).\1/g); ["p", "$blank$", "l", "$blank$", "c", "$blank$", ""]
what want
["p", "$blank$a$blank$",l, "$blank$a$blank$", "c", "$blank$e$blank$"]
this the docs suggest:
if separator regular expression contains capturing parentheses, each time separator matched, results (including undefined results) of capturing parentheses spliced output array.
the capturing parentheses include first $blank$
, that's gets included in array.
if want whole split string included, you'll need:
text.split(/(\$blank\$.\$blank\$)/)
["p", "$blank$a$blank$", "l", "$blank$a$blank$", "c", "$blank$e$blank$", ""]
the empty string @ end expected behavior when split()
finds delimiter @ end of string.
Comments
Post a Comment