I would like to split the string into %\d+or \n. I was able to successfully split one of these two, but not both:
> msg = 'foo %1 bar \n baz %2'
> msg.split(/(%\d+)/)
["foo ", "%1", " bar
baz ", "%2", ""]
> msg.split(/(\n)/)
["foo %1 bar ", "
", " baz %2"]
> msg.split(/(\n)|(%\d)/)
["foo ", undefined, "%1", " bar ", "
", undefined, " baz ", undefined, "%2", ""]
In the latter case, why undefinedin the resulting array, and what should I do?
Update . I forgot to indicate that I need delimiters. As a result, I want:
["foo ", "%1", " bar ", "\n", " baz ", "%2"]
source
share