I would like to split a string on either %\d+
or \n
. I was able to successfully split on either one of these two, but not on 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 last case, why is undefined
in the resulting array, and what should I be doing?
Update: I neglected to state that I need the delimiters. The result I want is:
["foo ", "%1", " bar ", "\n", " baz ", "%2"]
Quoting the MDN doc for String.prototype.split
:
If separator is a regular expression that contains capturing parentheses, then each time separator is matched, the results (including any undefined results) of the capturing parentheses are spliced into the output array.
The point is that any capturing group is spliced - even the one that misses the target. The first undefined
in your example is the 'nothingness' matched by \n
(split occured when %\d
matched), the second is for %\d
(when \n
was matched)... you see the picture.
To solve this, you can get rid of capturing groups (as alternation operator has the lowest precedence anyway):
msg.split(/\n|%\d/); // ["foo ", " bar ", " baz ", ""]
If you do need that separating parts as well, use just a single capturing group:
msg.split(/(\n|%\d)/);
// ["foo ", "%1", " bar ", "\n", " baz ", "%2", ""]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With