From Mozilla's JS ref:
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. However, not all browsers support this capability.
If the regex in split
contains capturing groups, the contents of each group is inserted in the result as well. Since you have a capturing group (któ)
, that is what you get. It is empty because (?!(któ))
is empty. If you add the text , któ
anywhere inside your string, you will see it appear:
var parts="olej sojowy, któ sorbitol, czerwień koszenilową".split(/, (?!(któ))/g);
shows 3 elements. The 2nd is, quite surprising, just ", "
. Then again, it is the one where któ
follows (not sure how I can "prove" that").
If you omit the parentheses inside your lookahead, it works as you expect it to:
var parts="olej sojowy, któ sorbitol, czerwień koszenilową".split(/, (?!któ)/g);
No capturing groups so you get only the remaining text after removal of the matching regex.