-1

I would like preserve dots using split function.

string exemple :

var str = "oqisjdf qosdifjq. fqs and the, fo. osidfo tchim cth."

If I use str.split('.'), I'll obtain :

["oqisjdf qosdifjq", " fqs and the, fo", " osidfo tchim cth", ""]

but this is what I need :

["oqisjdf qosdifjq.", " fqs and the, fo.", " osidfo tchim cth."]
kevin richard
  • 89
  • 1
  • 8
  • 2
    Does this answer your question? [Split string into array without deleting delimiter?](https://stackoverflow.com/questions/24503827/split-string-into-array-without-deleting-delimiter) – emerson.marini Oct 12 '20 at 11:07
  • Does this answer your question? [javascript - split without losing the separator](https://stackoverflow.com/questions/12721844/javascript-split-without-losing-the-separator) – Guy Incognito Oct 12 '20 at 11:08

1 Answers1

4

You could split by a positive lookbehind.

var string = "oqisjdf qosdifjq. fqs and the, fo. osidfo tchim cth.",
    array = string.split(/(?<=\.)/);

console.log(array);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392