JS string.split() without removing the delimiters [duplicate]

Try:

"abcdeabcde".split(/(d)/);

I like Kai's answer, but it's incomplete. Instead use:

"abcdeabcde".split(/(?=d)/g) //-> ["abc", "deabc", "de"]

This is using a Lookahead Zero-Length Assertion in regex, which makes a match not part of the capture group. No other tricks or workarounds needed.


Try this:

  1. Replace all of the "d" instances into ",d"
  2. Split by ","
var string = "abcdeabcde";
var newstringreplaced = string.replace(/d/gi, ",d");
var newstring = newstringreplaced.split(",");
return newstring;

Hope this helps.