I\'d like to split an input string on the first colon that still has characters after it on the same line.
For this, I am using the regular expression /:(.+)/<
From the ECMAScript 2015 spec (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. For example,
"Aboldand
coded
".split(/<(\/)?([^<>]+)>/)evaluates to the array:
["A", undefined, "B", "bold", "/", "B", "and", undefined, "CODE", "coded", "/", "CODE", ""]
Like in your example example, the output array here contains a trailing empty string, which is the portion of the input string past "coded"
that isn't captured by the separator pattern (which captures "/"
and "CODE"
).
Not obvious, but makes sense as otherwise the separator captures would end up in the end of the split array where they actually would not separate anything.