I would like to break up a sentence into words. If it only contains whitespaces, then .split(/\s+/)
works.
But how is it possible to split by comma as well, and also keep the comma in the resulting array?
I tried something like this, but it does not work:
.split(/(?=,)|(?!=,)|\s/)
Example input:
"this,is, a test"
Expected output:
["this", ",", "is", ",", "a", "test"]
What do I wrong? Is it even possible using regex only?