Split Strings and Keep the Delimiter

Posted on Updated on by Gergő D. Nagy

Splitting strings by a separator is a daily job for a developer. Usually, we don’t want to keep the delimiter, only the strings that have been glued by it. But still, there are some cases, when we want to keep them when splitting the string.

'one.two.three'.split(/(?<=\.)/); // ['one.', 'two.', 'three']

'one.two.three'.split(/(?=\.)/); // ['one', '.two', '.three']

You can see, we even have control, where to put the delimiter.

Need a web developer? Maybe we can help, get in touch!