I would like to split this text,
"Tom works. Tom is a student. Bob is a student."
into this,
["Tom", "works", ".", " ", "Tom", "is", "a", "student", ".", " ", "Bob", "is", "a", "student", "."]
I have tried text.split(/(\.)(\s)/)
but I am unsure how to add splitting on spaces without capturing them.
You can split on non-captured spaces, or on a captured period and optionally captured space, then filter out empty matches:
Another method, with
.match
instead ofsplit
, and using lookbehind: